wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes local class declaration problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "local class declaration problem?" Watch "local class declaration problem?" New topic
Author

local class declaration problem?

joe bob
Greenhorn

Joined: Feb 13, 2013
Posts: 5
Hi guys,

I have a little problem here. Why the following code would fail to compile at line 8 because it's not declared as final??


Thanks


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

joe bob wrote:
I have a little problem here. Why the following code would fail to compile at line 8 because it's not declared as final??


Thanks




First of all, do you understand why the code even compiles in the first place? After all, the scope of the x and z variable is only during the method execution, while the scope for the inner instance is til it is garbage collected -- so in theory, if your method returns the instance created in the method (and the stuff in the constructor is moved to a method), then doesn't the code in the method local inner class access variables that no longer exist?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
joe bob
Greenhorn

Joined: Feb 13, 2013
Posts: 5
The concept is a little bit off to me, could you explain a little more ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

joe bob wrote:The concept is a little bit off to me, could you explain a little more ?



Simply, it is possible for instances (of method local classes) created in a method to outlive the method execution itself -- the method can return the instance, or place it somewhere such as a static variable. This means that the method local class should never be allowed to access the local variables of the method.

However, as you know, if that restriction was used, then the benefit of using method local inner classes would be arguably degraded. And the Java designers understood this too.... Anyway, what if the value of the method local variable is constant and is known? Then you can argue that the inner class is really just using the value and not the local variable directly. The java language specification states that if the method variable is final *and* it is already set at the time of the class instantiation, then the method local inner class will make a copy of the value internally.

So back to your question...

joe bob wrote:
I have a little problem here. Why the following code would fail to compile at line 8 because it's not declared as final??


The question is backwards. It should be why the following code compiles when it is declared final -- as it should never compile at all. And the answer is, it compiles because the Java Language Specification allows the method local inner class to make a copy of the variable -- but only if the value is constant and is known.

Henry
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 775
It has something to do with garbage collection and the variable scope in a method.

In KB's study guide, if a method local inner class wants to access the method's variable, that variable must be final.
Suppose this inner class has an instance and this instance is passed to other methods as argument. However, the variable that this instance encapsulated is garbage collected after the method finishes. That will be a problem.
If this variable is set to be final, then it won't be garbage collected even if the method completes. This variable can still be used by the inner class instance.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: local class declaration problem?