• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method-local inner class and final local variable

 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry Wong,

Great Job. I got the picture clear.
Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic