File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method local inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method local inner class" Watch "method local inner class" New topic
Author

method local inner class

pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
method local inner classes are just like method members . so it may be final . because it is class so it may be abstract . but why this class can't access methods members .

thanks in advance .
bye , pooja


:d
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A class defined within a method can access local method variables only if they're final. Otherwise, the inner class object might outlive the local method variables which only exist during the scope of the method and are typically destroyed when the method exits. But if the local variables are final, then the compiler can copy their values into the inner class object.


"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
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
final variable are also destroyed after method termination ( i think ) .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by pooja jain:
final variable are also destroyed after method termination ( i think ) .

Yes, but by then they've also been "inlined" as part of the locally defined object. This could not happen if the variable wasn't final.
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
marc , please explain it in simple word . what a final variable can do that non final can't do . if you have any anology then it would be more batter .
thanks , pooja
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Variables don't do anything. Variables are the nouns, methods are the verbs. A final variable is one that cannot be changed unless it's uninitialized when declared, in which case it must be initialized in a constructor or initialization block.

I hope this helps...


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
Jeff you got the wrong or my question was not clear . I meant was what a final variable can do for method local inner class that non final variable can't do .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
what is the meaning of this final variable can be copied in object .
please help . thanks .
pooja .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The value of a final variable cannot change, so that value (not the variable) can be integrated into the object as a "constant."

In the example below, x is a local variable that currently holds the value 1. But x is not final, so that value might change later. If a LocalClass object is created, that object might continue to exist after the method has exited. And so if LocalClass had been allowed to use x, then there would be a problem, because x would no longer exist and the object would have no way of knowing whether the value 1 is still valid.

On the other hand, y is also a local variable. But y is final, so its value of 2 cannot change. Now if a LocalClass object is created, it can simply copy the value of y. When the method exits and y is destroyed, that's okay because the object doesn't have to go back to y for its value. The object uses a "hard-coded" value of 2 instead of the variable y.

(With objects, of course, we're talking about references being final. So if refA is final, then refA will always "point" to the same object.)

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method local inner class
 
Similar Threads
Regarding Inner Class
Why local inner classes CAN "see" final variables?
Anonymous inner Classes accessing final variables
Method-Local Static Inner Class
inner class static or no?