In the case of a final instance variable, the initial assignment of a value must happen before the constructor completes. Can anyone explain why the following code snippet compiles and runs, when initialization of the final method local variable doesn't occur when the variable is declared?
The following snippet is from K&B SCJP 5 Study Guide (Chapter 5 pg. 324)
And technically, this follow-up error has nothing to do with the first error -- is a completely different question.
Hope this helps,
Joined: Jun 17, 2009
Henry Wong wrote: case statements require compile time constants
JLS wrote:4.12.4 final Variables
A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.
[BLAH BLAH BLAH]
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).
The final variable (in this case) is initialized at run-time as the code executes, not compile-time when it is declared. I think that is what you are trying to say?
subject: assigning a final value to a method local variable