• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access modifier for members of method local inner class.

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As Method local inner class are accisible only within the method in which its declared and hence a method local inner classs is not visible outside the method in which its declared. As a result of this it does not make any sense to have access like public,protected and default access to the members of the method local inner class because we can never pass a reference to a method local inner class instance to any other method because the inner classs will not be visible outside.
Is my understanding correct? If yes then why does compiler allow the memebers of the method local inner class to have above modifiers?
Thanks
Deepak
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could have one method local inner class extend another, in which case it would matter whether your members are private or not. For example, this compile isn't legal:
You have to change the access modifier of Bar.bar to default, protected, or public for the above program to compile.

(I do agree that this is rarely useful in practice, though.)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Often, local classes may implement interfaces, in which case they must implement public methods. Likewise in inner class may extend another class (not just another inner class) - in which case, it must use appropriate access modifiers when overriding methods.

It's true that there may be many methods and fields of local classes for which access modifiers are irrelevant. I usually just omit modifiers in this case, rather than typing "public" or "private" and letting the reader wonder what that means. Of course, a default modifier means something too. But the point is, if you need a particular type of modifier, use it, and if it doesn't matter what modifier you use, don't worry about it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic