wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why local class objects can live longer? 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 "Why local class objects can live longer?" Watch "Why local class objects can live longer?" New topic

Why local class objects can live longer?

Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133
Local class can access only final variables defined inside the method (including method arguments). This is because the class can outlive the method, but the method local variables will go out of scope - in case of final variables, compiler makes a copy of those variables to be used by the class.

I try to use the local class object after its enclosing method dies in the following code, but without success. Can someone give me an example to show how local class outlive its enclosing method?

Sun Certified Java Programmer
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
I have made some changes to the original code
[B] [/B]]
check u r code how can u refer the variable 'in' in another method after itz scope is over.
now for u r original qiestion. u can return the object of the inner class from a method and that object can be accessed after the method is over. please check the code above
please dont try to access the variable directly instead to make the concept clear use a method inside the inner class and access it from outside for that u have to first define an interface which defines that method.
I want to add one more question to this discussion is there any use of an inner local class if i dont extend from some other class or implement an interface from some other context.Because i wont be able to refer to the local iner class in any other context.ofcourse i can use it inside the method but i dont find much use of doing that
[This message has been edited by Cherry Mathew (edited January 16, 2001).]
[This message has been edited by Cherry Mathew (edited January 16, 2001).]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Why local class objects can live longer?
Similar Threads
class types
Inner class basics. (Marcus Green's mock exam 2)
Pls help- write a code with anonymos class, indicate with a comment line in the code
method-local inner class problem