~ Mansukh
Mansukhdeep Thind wrote:Answer these questions first:
a) What type of variable is "a"?
b) Where is variable "a" being stored i.e in which part of the memory?
~ Mansukh
Mansukhdeep Thind wrote:Correct. Now if "age" is a local method instance variable and is on the stack of that method, then what will happen once the method has completed its execution? What will happen to the stack?
~ Mansukh
Mansukhdeep Thind wrote:What did you get? Can you please write it down? I just want to be sure you got the right thing. Why can't I access any local variable from a method local inner class unless it is declared as final? Why is the compiler complaining?
Mansukhdeep Thind wrote:Correct. Now if "age" is a local method instance variable and is on the stack of that method, then what will happen once the method has completed its execution? What will happen to the stack?
~ Mansukh
~ Mansukh
~ Mansukh
Ranjith Suranga wrote:Further more, I actually don't what is called "local instance variable" ? Can you explain please ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Ranjith Suranga wrote:Further more, I actually don't what is called "local instance variable" ? Can you explain please ?
~ Mansukh
Winston Gutkowski wrote:
Ranjith Suranga wrote:Further more, I actually don't what is called "local instance variable" ? Can you explain please ?
I don't think there's any such animal. What you have is a local variable.
Furthermore, the specific problem you're running into can be fixed by simply writing your class just as you would any other, viz:then you can declare your local variable any way you like, because its value won't be needed outside its normal scope.
Sometimes the simplest solution is the best.
Winston
~ Mansukh
Winston Gutkowski wrote:
Furthermore, the specific problem you're running into can be fixed by simply writing your class just as you would any other, viz:then you can declare your local variable any way you like, because its value won't be needed outside its normal scope.
Sometimes the simplest solution is the best.
Winston
Ranjith Suranga wrote:"I said no.. you can't" because
Look at line 19 , it should generate compile time error...
and honestly...
I don't know how to create instance or reference type from local inner class outside of the block, where inner class has been declared... ? (as you have done here in line 24)
~ Mansukh
Mansukhdeep Thind wrote:Let me know in case you still have some doubts.
Mansukhdeep Thind wrote: the compiler will complain if I try to access that variable using inner class reference unless it is marked as final. Only then will the inner class instance get its own copy of the instance variable..
Ranjith Suranga wrote:How can I access "int age = 10" in Local Inner Class... ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Ranjith Suranga wrote:
Mansukhdeep Thind wrote:Let me know in case you still have some doubts.
Yes dear, I still have some doubts...
Mansukhdeep Thind wrote: the compiler will complain if I try to access that variable using inner class reference unless it is marked as final. Only then will the inner class instance get its own copy of the instance variable..
Should we want to guess it or has it been specified in JLS or somewhere ?
~ Mansukh
Ranjith Suranga wrote:Should we want to guess it or has it been specified in JLS or somewhere ?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.
Any local variable used but not declared in an inner class must be definitely assigned (ยง16) before the body of the inner class.
Mansukhdeep Thind wrote:
I did not get your question. Can you re-phrase?
Mansukhdeep Thind wrote:I wanted the lad to discover it for himself as to why we cannot access local variables from inner classes unless they are marked as final. That was his question.
Your solution is simple no doubt. But it does not answer his question.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mansukhdeep Thind wrote:
Marking the local variable as final inside a method which has an inner class allows the Java compiler to "capture" the value of the variable at run-time and store a copy as a field in the inner class. Once the outer method has terminated and its stack frame has been removed, the original variable is gone but the inner class's private copy persists in the class's own memory.
Ranjith Suranga wrote:
Mansukhdeep Thind wrote:
I did not get your question. Can you re-phrase?
I mean your explanation, It feels really OK and I found something similar to like your explanation also here...
http://techtracer.com/2008/04/14/mystery-of-accessibility-in-local-inner-classes/
But, I want to know is, How can I know this.. I mean it should be specified in somewhere ?
I could not able to find it in JLS also...
~ Mansukh
Ranjith Suranga wrote:
Mansukhdeep Thind wrote:
Marking the local variable as final inside a method which has an inner class allows the Java compiler to "capture" the value of the variable at run-time and store a copy as a field in the inner class. Once the outer method has terminated and its stack frame has been removed, the original variable is gone but the inner class's private copy persists in the class's own memory.
Where this has been specified... ? can you tell me... please ?
~ Mansukh
Mike Simmons wrote:No, he's asking about the specification. Try this, from JLS 8.1.3:
Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.
~ Mansukh
Any local variable, formal parameter, or exception parameter used but not declared
in an inner class must be declared final.
Mike Simmons wrote:OK. Then I would ask Ranjith: why should the explanation need to be specified anywhere? That's not the job of a specification. They may choose to explain, but they don't have to.
Another way to look is: Java's creators certainly could have specified this differently. Other languages allow similar references by implicitly creating closures, constructs to capture the state of local variables and pass them outside their original lexical scope. Java's designers simply chose not to do this, probably because they felt is would be too complicated. Even now, with Java 8 approaching with the inclusion of lambdas, which are concise local function declarations for purposes similar to anonymous functions, they still do not plan to allow lambdas to access nonfinal local variables. Maybe they will in the future. But don't count on it. At some opoint, we sometimes have to accept that some things are the way they are because the people who created the language chose to make it that way.
~ Mansukh
OCPJP
Local variables on the heap? Local variables live on the stack, but they may be pointers to objects on the heap.Ishan Pandya wrote: . . . that final variables may be stored as a special part of the heap called the "permanent generation". . . .
Campbell Ritchie wrote:Local variables on the heap? Local variables live on the stack, but they may be pointers to objects on the heap.
OCPJP