Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Java Final variables have default values?

Tags:

java

final

I have a program like this:

class Test {      final int x;      {         printX();     }      Test() {         System.out.println("const called");     }      void printX() {         System.out.println("Here x is " + x);     }      public static void main(String[] args) {         Test t = new Test();     }  } 

If I try to execute it, i am getting compiler error as : variable x might not have been initialized based on java default values i should get the below output right??

"Here x is 0". 

Will final variables have dafault values?

if I change my code like this,

class Test {      final int x;      {         printX();         x = 7;         printX();     }      Test() {         System.out.println("const called");     }      void printX() {         System.out.println("Here x is " + x);     }      public static void main(String[] args) {         Test t = new Test();     }  } 

I am getting output as:

Here x is 0                                                                                       Here x is 7                                                                                      const called 

Can anyone please explain this behavior..

like image 896
user3766874 Avatar asked Jul 28 '14 07:07

user3766874


People also ask

What is the default value of final variable in Java?

The uninitialized default value (null or zero) can sometimes be observed by code which has access to the containing object (or class, if the variable is static). But normally, a final variable is initialized shortly it is created. The special case of a blank final variable is one which has no initializer expression.

Do Java variables have default values?

The local variables do not have any default values in Java. This means that they can be declared and assigned a value before the variables are used for the first time, otherwise, the compiler throws an error.

Do final variables have to be initialized?

A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable.

What happens if the variable is final in Java?

Once any entity (variable, method or class) is declared final , it can be assigned only once. That is, the final variable cannot be reinitialized with another value. the final method cannot be overridden.


1 Answers

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html, chapter "Initializing Instance Members":

The Java compiler copies initializer blocks into every constructor.

That is to say:

{     printX(); }  Test() {     System.out.println("const called"); } 

behaves exactly like:

Test() {     printX();     System.out.println("const called"); } 

As you can thus see, once an instance has been created, the final field has not been definitely assigned, while (from http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.2):

A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.

While it does not seem to be stated explitely in the docs (at least I have not been able to find it), a final field must temporary take its default value before the end of the constructor, so that it has a predictable value if you read it before its assignment.

Default values: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5

On your second snippet, x is initialized on instance creation, so the compiler does not complain:

Test() {     printX();     x = 7;     printX();     System.out.println("const called"); } 

Also note that the following approach doesn't work. Using default value of final variable is only allowed through a method.

Test() {     System.out.println("Here x is " + x); // Compile time error : variable 'x' might not be initialized     x = 7;     System.out.println("Here x is " + x);     System.out.println("const called"); } 
like image 72
sp00m Avatar answered Oct 11 '22 20:10

sp00m