| Author |
static final
|
Max White
Ranch Hand
Joined: Jun 28, 2008
Posts: 83
|
|
Hi,
When I use the following code ..
or the following
The code gets compiled.
But not when I use the following
Error : Final variable may not have been initialised.
My doubt is that it works if I just use either a final or a static variable and the variable gets the default value of null.
Why does it not work with static final ?
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
final instance/class variable of class should assign a value in constructor(other word on declaration) else error.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
you know unfortunately final modifier is unnoticed between developer except in context of constant....
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
from the JLS
8.3.1.2 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
declared.
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.
|
OCPJP 6(100 %) OCEWCD 6(91 %)
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
gurpeet reply is good ...
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
Seetharaman Venkatasamy wrote:gurpeet reply is good ...
or you should say JLS's reply is good
|
 |
Praveen Kumar M K
Ranch Hand
Joined: Jul 03, 2011
Posts: 256
|
|
Case 1 :
Max White wrote:
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.
Case 2:
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.
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
hello Praveen , i have to correct you in case 1 .
contrary to what you said, Case 1 also won't compile. i'm quoting again what i wrote above from the JLS
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.
here definitely assigned means that we should assign it either while declaring it or in the constructor or in the instance initializer.
the code snippet in case 1 wont compile so there won't be any default value assignment to String instance member
the same thing holds for case 2 as well.
regards
|
 |
Praveen Kumar M K
Ranch Hand
Joined: Jul 03, 2011
Posts: 256
|
|
|
Yes, I stand corrected! You need to assign a value before the first use of the variable(and not the default null).
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
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
Ranch Hand
Joined: Jul 03, 2011
Posts: 256
|
|
|
Yeah, I meant that I was wrong(concerning Case 1), thanks for the correction!
|
 |
gurpeet singh
Ranch Hand
Joined: Apr 04, 2012
Posts: 867
|
|
|
you are welcome buddy
|
 |
Max White
Ranch Hand
Joined: Jun 28, 2008
Posts: 83
|
|
Thank you all for the answers.
Gurpeet is right,case 1 wont compile.Its my mistake.
|
 |
Mrinal Singhania
Greenhorn
Joined: Sep 30, 2012
Posts: 7
|
|
|
 |
 |
|
|
subject: static final
|
|
|