Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class question...

 
Andrea Gazzarini
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • 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'.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic