GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assigning a final value to a method local variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assigning a final value to a method local variable" Watch "assigning a final value to a method local variable" New topic
Author

assigning a final value to a method local variable

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
'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: 206
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>


Thanks,
Harry Henriques
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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...

http://www.coderanch.com/t/454384/Beginning-Java/java/What-compile-time-constant



And technically, this follow-up error has nothing to do with the first error -- is a completely different question.

Hope this helps,
Henry
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
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.
 
GeeCON Prague 2014
 
subject: assigning a final value to a method local variable