• Post Reply Bookmark Topic Watch Topic
  • New Topic

Non-public Outer Class Versus Static Inner Class  RSS feed

 
Ranch Hand
Posts: 224
2
  • 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
 
Saloon Keeper
Posts: 8457
155
  • 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.
 
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
Saloon Keeper
Posts: 8457
155
  • 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
Saloon Keeper
Posts: 8457
155
  • 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.
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!