• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Inner classes

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey grp-
Is it possible to access the method-local inner class methods from outer classes?. If so, then how?. Please reply me. Thanks.
Maha
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch Maha!
The enclosing class could invoke the method in which the class is declared and created. If the method returns a reference to a new instance of the method local class, then the enclosing class can invoke a method on the new instance using the returned reference.
I'm sure that really isn't the answer that you are looking for. The correct answer is "no". The enclosing class has no way to invoke a method on a method local class unless a reference to the new instance of the method local class is made available to the enclosing class.
I hope I haven't made that even more confusing than it was previously.
 
maha prataps
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your prompt reply Dan. Actually K& B suggested (as you did) that using the reference variable we can access the method-inner class methods. But when I tried to code the same, compiler scolded me. That made me to post the question in this forum. However, I too think that the method-inner class methods are inaccessable. Any other comments ?. Thanks again.
Maha
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just have a look at the following Code.
Here the reference of the class is returned to the Outer class. Using this reference you can call any method of the 'method local inner class'.

This is one of the reason for not allowing the 'method local inner class' to have access to local variables of the method.
Hope this helps!
Regards,
Hari
[ November 29, 2003: Message edited by: Hari G ]
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hari. Just some clarifications.


This is one of the reason for not allowing the 'method local inner class' to have access to local variables of the method.


method local inner class can access local variables of the method just as long as it is declared final.
 
H Gokulam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dennis,
Yes! You are right!.
Method local inner classes can access final variables of the enclosing method.
Regards,
Hari
 
maha prataps
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks hari for such a wonderful explanation.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by maha prataps:
Thanks hari for such a wonderful explanation.


hello,
I steped in, coz i have got some querry::: why do we need access modifiers for the method-local inner classes. I have read somewhere that they can have final and abstract as their modifiers, obviously both cannot be applied together. Pls throw some light on this.
 
H Gokulam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have read somewhere that they can have final and abstract as their modifiers, obviously both cannot be applied together.


Yes, You are perfectly right! (BTW. abstract and final are not acces modifiers)

why do we need access modifiers for the method-local inner classes


The class definition will not be available outside the method. So I don't have any clear explanation to your doubt. In case anybody has any justification please post!
Regards,
Hari.
 
Danger, 10,000 volts, very electic .... tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic