This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 9255
177
  • 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: 9255
177
  • 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: 9255
177
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!