Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assigning a final value to a method local variable

 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic