188.8.131.52 final Fields A field can be declared final (§4.12.4). Both class and instance variables
(static and non-static fields) may be declared final.
It is a compile-time error if a blank final (§4.12.4) class variable is not definitely
assigned (§16.8) by a static initializer (§8.7) of the class in which it is
A blank final instance variable must be definitely assigned (§16.9) at the end
of every constructor (§8.8) of the class in which it is declared; otherwise a compile-
time error occurs.
How do we get to use this final variable? We would have to create a Test object through "new Test()" and this will further call the parameter-less constructor which would set the value of a to default null. You would have no problem.
Max White wrote:
In this case can we access the final variable without a constructor? The answer is yes. Final variables not assigned during declaration are called blank final variables and its a compile time error to use them without assignment. The only difference with Case 1 is the fact that the assignment is taken care in the constructor call.
Praveen Kumar M K wrote:Yes, I stand corrected! You need to assign a value before the first use of the variable(and not the default null).
i do not know what you are trying to say. i may be wrong in interpreting you . all i want to say is that case 1 won't compile. you have to explicitly initialize final instance or class variables in their respective initializers.
Praveen Kumar M K
Joined: Jul 03, 2011
Yeah, I meant that I was wrong(concerning Case 1), thanks for the correction!