Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non-public Outer Class Versus Static Inner Class

 
Kevin Simonson
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between having a class that isn't public in a file with another class that is public, and having a static inner class inside a public class? Why would one use one and not the other?

For example, say I have a file like so:



and another file like so:



What are the different situations where I'd want to use one of these files over the other?

Kevin Simonson
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never use the former. If you have two different top-level classes, they should be in different files. This makes it much easier to find them when you're looking for source code.

The second case is useful if the two classes are very tightly coupled, or the one class is just logically an internal part of the other class.

For instance, a GUI class could have internal classes that are event handlers. In this case, the two classes are very tightly coupled and the event handler can benefit from accessing the fields of the enclosing class directly.

An example of where one class could logically be an internal part of its enclosing class, is a ChessPiece which has an internal class Color. This way, a ChessPiece.Color is different from a Rainbow.Color.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading a book that says the good use for an inner class is when that class it will only helps the public class of that file.

I see no point in putting a class in the same file of the other and leave it public to the world.

In the book also says that if the inner class is privated you could leave its attributes public. [=

The book is: Effective Java 2rd Edition - Joshua Block and it talks a lot about inner classes.
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hebert Coelho wrote:In the book also says that if the inner class is privated you could leave its attributes public. [=


Yes, but there is no reason to do this. Better get into the good habit and make everything private always.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Hebert Coelho wrote:In the book also says that if the inner class is privated you could leave its attributes public. [=


Yes, but there is no reason to do this. Better get into the good habit and make everything private always.

I do not know in pratice how it would be, but in the book he shows it as a good reason. You would reduce the number of the methods of the inner class. Once the inner class is private, the only one with access is the public class of the file.

Again, I do not worked with a system with inner classes yet so I do not know the impact of this kind of approach.
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The outer class can access members of the inner class regardless of their access modifier. So just make them private. Either way, there is no need for accessor methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic