• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class Compile Time Constant

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5594
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21019
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic