aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method-local inner class and final local variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method-local inner class and final local variable " Watch "Method-local inner class and final local variable " New topic
Author

Method-local inner class and final local variable

Benjamin Samuel
Ranch Hand

Joined: Dec 15, 2006
Posts: 33
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

Joined: Aug 31, 2004
Posts: 11343

If a local variable is final, then its value can be copied directly into the instance.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Benjamin Samuel
Ranch Hand

Joined: Dec 15, 2006
Posts: 33
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

Joined: Feb 15, 2007
Posts: 557
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.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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

Joined: Dec 15, 2006
Posts: 33
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

Joined: Feb 15, 2007
Posts: 557
Hi Henry Wong,

Great Job. I got the picture clear.
Thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method-local inner class and final local variable