• 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

inner class question...

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!
If I have a private inner class what modifier must I assign to this class members (constructor, variables and methods)??
Thanks
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code will still compile no matter what access modifier you use for the members. However, since it is a private inner class, only the enclosing class will have access to it and its members. So I guess it comes down to what you think expresses your intent more clearly.
Moving this to Java in General (Beginner)
 
Andrea Gazzarini
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I would know is :
If I have a private Inner class like this:
public Outer{
...private class Inner{
......
......XXX String aVariable;
......
......XXX Inner(){}
......
......XXX void doSomething(){}
......
......XXX
...}
}

what is the correct modifier to assign to the members (in place of XXX)?
I know that every modifier compile fine, but I would know what's the better modifier (XXX = private ?)
What sense to make all modifier, for example, with default modifier??
Thanks
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think (if I understand correctly) there isn't a 'correct' choice in this case. Since it is an inner class the only class which has access to the methods is the enclosing class. It doesn't matter if you make the methods 'public' or 'private'.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rewording William's response a bit and merely repeating Junilu:
Since it is a private inner class the only things that have access to its members are itself and the other members of the enclosing class - no matter the access modifiers of the inner class' members.
[ October 22, 2002: Message edited by: Dirk Schreckmann ]
reply
    Bookmark Topic Watch Topic
  • New Topic