my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class inside method 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 "Class inside method" Watch "Class inside method" New topic
Author

Class inside method

Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi,
If I declare and define a class inside a method, I can access only the final local variables of the method, isn't it? The reason given in RHE for this is that the object of the class declared inside method should be able to outlive the method and this is possible only if variables that can be accessed from the method are final. But then, I am not able to create/access an object of the class declared inside the method outside the method at all!!! Then, what's the big deal in allowing only final variables of the method to be able to be accessed??? Or is there any way that I can create/access an object of the class declared inside a method outside the method??
Thanks,
Aparna
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

that is a good question. that went through my mind before too. all i know right now is a local class can only have "friendly" access. im fairly sure of this.


SCJP
Visit my download page
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi Randall,
Thanks for ur reply. But then, such a thing is not discussed even in JLS, where else to look for answers? Everywhere, the examples comfortably declare an object inside the method itself. There has been no example/ no mention of how that object can be accessed outside. Any help from anyone would be well appreciated.
Thanks,
Aparna
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Class inside method
 
Similar Threads
an urgent inner class question
Innerclass Qn
FAQ Explanations
what can a static local class access ?
Inner classes and 'final'