How to Create a Final Variable in Java


In this example we will show the method to create a final variable in Java.

Source Code

package com.beginner.examples;

public class CreateFinalVariable {
  public static void main(String[] args) {
    final int i = 3;
    i = 7; // will compilate failed
    System.out.println(i);
  }
}

Output:

error: cannot assign a value to final variable i
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments