assigning a final value to a method local variable
Harry Henriques
Ranch Hand
Joined: Jun 17, 2009
Posts: 204
posted
0
Hello,
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)
Thanks,
Harry Henriques
This message was edited 3 times. Last update was at by Harry Henriques
Instance variables and local variables are not the same thing.
For local variables, it must be assigned only once, and before it is used. You also have to be careful with conditionals and loops, as reachability rules can affect it too.
Henry
This message was edited 1 time. Last update was at by Henry Wong
'b' is not an instance variable.
'b' is a local variable in the main method.
For local variables in methods there is other rule: 'value must be assigned before first use'.
Harry Henriques
Ranch Hand
Joined: Jun 17, 2009
Posts: 204
posted
0
Thank you for the explanation. I have a follow-up question from the same example. Why doesn't the following code snippet compile?
Ireneusz Kordal wrote:'value must be assigned before first use'
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Harry Henriques>vim Test.java
C:\Documents and Settings\Harry Henriques>javac Test.java
Test.java:10: constant expression required
case b: System.out.println("case b");
^
1 error
C:\Documents and Settings\Harry Henriques>
Harry Henriques wrote:Thank you for the explanation. I have a follow-up question from the same example. Why doesn't the following code snippet compile?
As explained in the error message, case statements require compile time constants. And the variable b is not a compile time constant.
BTW, I wrote a description of what is a compile time constant some time ago...
And technically, this follow-up error has nothing to do with the first error -- is a completely different question.
Hope this helps,
Henry
This message was edited 2 times. Last update was at by Henry Wong
Harry Henriques
Ranch Hand
Joined: Jun 17, 2009
Posts: 204
posted
0
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?
Thanks Henry.
This message was edited 2 times. Last update was at by Harry Henriques
subject: assigning a final value to a method local variable