• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Method-local inner class and final local variable

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is the code from K&B in the method-local inner classes chapter

It is said in the book that since 'z' is a method local variable (and hence placed on the stack), it wont be accessible by the inner class. But if we make it a final variable (final String z = "local variable"; ) , then the inner class can access it! How? Is the final variable placed on the heap? Won't it lose its scope once the method finish running?
[ February 28, 2007: Message edited by: Binil Benjamin ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a local variable is final, then its value can be copied directly into the instance.
 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what if we call the seeOuter() method using an object of inner class from outside the method doStuff()? How can it print the value of z?
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Benjamin,
I got your point.
I think the fallowing para in K&B makes no sense.
But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable.

1)How can anybody imagine a Method Local Inner class reference passing out side the method in which its declared. I bet we can not.
2)I don't see any reason why Method Local Inner class can't access Local variables.
3)"First of all the purpose of Method Local Inner classes in not clear."

Anybody who has experimented with Inner classes please respond.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1)How can anybody imagine a Method Local Inner class reference passing out side the method in which its declared. I bet we can not.



Not sure what you mean. Are you saying that it can't be done, or that we can't imagine a use for it?



Henry
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasan thoyyeti:
... How can anybody imagine a Method Local Inner class reference passing out side the method in which its declared. I bet we can not...


 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

2)I don't see any reason why Method Local Inner class can't access Local variables.



If you agree that point #1 is false, and that you can pass instances of method (and anonymous) inner classes, outside of the method, then you can conclude that it is possible for local variables to go out of scope, when the instance is still in scope.

(Wow, long maybe run-on sentence )

In fact, instances of method inner classes can never access local variables. When the local variable is declared as final, a copy is made for the instance, because it is known to not change.

Henry
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

3)"First of all the purpose of Method Local Inner classes in not clear."



Okay, I will admit it. I am one of those people who abuse the use of anonymous inner classes. It is pretty silly to have a separate class definitions located elsewhere, when the class is only a few lines long, and used in one method.

Never really had the need for method local inner classes, as you can't really do a good job of abuse, if you spread it out ... ... seriously, there is some advantage of method inner classes over anonymous inner classes. The two that I can quickly think of are, you can implement multiple interfaces, and can declare constructors for it.

Henry
 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you mean to say that the compiler will replace the final variable with its constant value, even without considering its scope?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry Wong,

Great Job. I got the picture clear.
Thanks a lot.
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic