This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
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
Author

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
Cherry
[This message has been edited by Cherry Mathew (edited January 16, 2001).]
[This message has been edited by Cherry Mathew (edited January 16, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why local class objects can live longer?