-4

Below is the code where I have initialized twice the final variable. But I have read that final variable can be initialized only once. Can anyone explain me why I can initialize two different value for a single final variable.

class outer
{  
    private final int x;

    public outer(){
        x = 8;

    }
    public outer(int value){
        x = 9;
    }

    public static void main(String args[]){
        outer ot = new outer();
        System.out.println(ot.x);


}
}
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
Soumya
  • 233
  • 2
  • 9

3 Answers3

1

Actually in Java final keyword has two meaning, if you are using in classes and method then it is about inheritance and if it is about variables then final local variables may be accessed from anonymous inner subclasses, whereas non final local variables may not.

So in your case you are not initializing it again rather you are accessing the value of x inside your main method which is ok.

Check Important points on final in Java

  1. Final keyword can be applied to member variable, local variable, method or class in Java.
  2. Final member variable must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error.

  3. You can not reassign value to final variable in Java.

  4. Local final variable must be initializing during declaration.

  5. Only final variable is accessible inside anonymous class in Java.

  6. Final method can not be overridden in Java.

  7. Final class can not be inheritable in Java.

  8. Final is different than finally keyword which is used on Exception handling in Java.

  9. Final should not be confused with finalize() method which is declared in object class and called before an object is garbage collected by JVM.

  10. All variable declared inside java interface are implicitly final.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

I ran your code with a slight change and output is

8

9

public class Main {

  private final int x ;

    public Main(){
        x = 8;

    }
    public Main(int value){
        x = 9;
    }

    public static void main(String args[]){
        Main ot = new Main();
        System.out.println(ot.x);



        Main ot2 = new Main(11);
        System.out.println(ot2.x);
    }

}

Anyone correct me if i am wrong but i think with each object you get a new reference to the class

EDIT: if you try to set ot.x = 15 after the ot object you will get the following error

The final field Main.x cannot be assigned

Neo
  • 1,359
  • 14
  • 34
-1

You only ever initialize it once, depending on which constructor is called.

The control flow of the program can only ever pass through one constructor for each object initialization. This means that the value is assigned to the final variable only once per execution of the program.

"Final can only be assigned to once" means that the variable can only be assigned once per execution of the program, not that it can only be set in once place in the code.

ose
  • 4,065
  • 2
  • 24
  • 40