This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class Compile Time Constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class Compile Time Constant" Watch "Inner class Compile Time Constant" New topic
Author

Inner class Compile Time Constant

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi,
1. Even though Option 2 is not a compile time constant, How I am able to execute this program. I think only commented portion of Option 3 is compile time constant.
2. What makes final variable(Compile time constant) to work here ? Even though after method is completed and stack frame is blown away, all variables including final variables in method declaration should be Garbage collected know ? If that is the case, what is the difference of final and non-final variable declaration ? Is that my assumption correct ?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

It is legal because the variable isn't used anywhere before it has its value assigned to it, and it is always assigned that value (the program wouldn't compile if the assignment was in an if block, for instance.

Simply put, the compiler can tell by reading the program that you essentially wrote final int innerVariableInt = 20, even if you did it on separate lines. This only works for local variables, not for instance variables.

What's the use of final local variables? Simplicity is one. If you declare a variable as final, it will be more easy for people reading your code to determine what it does, as the value can't change. I also believe that Java can perform small optimizations on variables declared as final. The most used application however, is probably when you're using a local or anonymous class:


This code will only compile if myInt is declared final. You can only use local variables in another local class if they are declared final.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

Saibabaa Pragada wrote:
1. Even though Option 2 is not a compile time constant, How I am able to execute this program. I think only commented portion of Option 3 is compile time constant.


There is no compile-time-constant requirement for a local variable to be accessed by an inner class. The local variable simply has to be final, and already initialized by the time the inner class is instantiated.

Saibabaa Pragada wrote:
2. What makes final variable(Compile time constant) to work here ? Even though after method is completed and stack frame is blown away, all variables including final variables in method declaration should be Garbage collected know ? If that is the case, what is the difference of final and non-final variable declaration ? Is that my assumption correct ?


It's actually, not using the local variable that is being used. The compiler generates code that copies the value of the local variable to the inner class instance, which is then used by the inner class. And since the local variable is final, and already initialized, then the value shouldn't change right?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner class Compile Time Constant
 
Similar Threads
David Smalley
Where the variables are stored?
Method-local inner class & final variables
Are the following the ONLY ways to instantiate a non static inner class
final variables initialization