aspose file tools*
The moose likes Java in General and the fly likes Are public nested classes a good practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Are public nested classes a good practice?" Watch "Are public nested classes a good practice?" New topic
Author

Are public nested classes a good practice?

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hey all,

This question is regarding the public nested classes. If someone creates a public nested class inside it's outer public class, it works without any issue. But, is that is a good programming practice? According to SCJP 6 book, one source file can have one public class. OK, even if it works, my question is, that is a good programming practice? Because we know there are number of things in Java which can be countered as legal, but still not the way to do it. Please help.


Are you better than me? Then please show me my mistakes..
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

If the nested class is tightly coupled to the outer class and has no meaning or usage in another context, then I'd say it is OK. See for example the java.util.Map.Entry interface (well, this is an interface nested in outer interface, but the concept is the same).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8418
    
  23

Yohan Weerasinghe wrote:If someone creates a public nested class inside it's outer public class, it works without any issue. But, is that is a good programming practice? According to SCJP 6 book, one source file can have one public class. OK, even if it works, my question is, that is a good programming practice?

As Martin said, if it reflects model you're trying to achieve, it's absolutely the right thing to do. I use them a lot (although that's not necessarily a recommendation ).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Seems I'm ever the contrarian. :/ By enclosing one class inside another we are in effect saying "This is a thing which has no utility or meaning outside the context of the enclosing class." So what good does it do to make that thing public? Unless the class is static, we can't even instantiate it independently of an instance of its enclosing class. So what are the circumstances that make this a good idea?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Dennis Deems wrote:Seems I'm ever the contrarian. :/ By enclosing one class inside another we are in effect saying "This is a thing which has no utility or meaning outside the context of the enclosing class." So what good does it do to make that thing public? Unless the class is static, we can't even instantiate it independently of an instance of its enclosing class. So what are the circumstances that make this a good idea?

Though I'm certainly abusing public nested classes in my projects , I think that the Map.Entry interface is a good example. Should the Map.Entry interface be made a full blown independent public interface in your opinion?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

Here's an example of where I use a public nested class. The public nested class is a Builder, which is used to build an immutable Example object. A major advantage of making it a public nested class is that the constructors of both Builder and Example can be made private.

Here's an example of how to use it:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Thanks Martin & Jesper, those are both great examples! I recant.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

wow! That's a great knowledge to have!! Thanks all!! I will keep this thread open for anyone who need to debate on this issue
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8418
    
  23

Jesper de Jong wrote:Here's an example of where I use a public nested class. The public nested class is a Builder...

Great point. I use them all the time myself too; just didn't connect it as an illustration for OP's question.

Winston
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Dennis Deems wrote:By enclosing one class inside another we are in effect saying "This is a thing which has no utility or meaning outside the context of the enclosing class."


It's more like "This is a thing which depends on the context of the enclosing class", isn't it? Just because that dependence exists doesn't make the thing useless or meaningless. But I see we've already had some examples which are useful or meaningful.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Winston Gutkowski wrote:
Jesper de Jong wrote:Here's an example of where I use a public nested class. The public nested class is a Builder...

Great point. I use them all the time myself too; just didn't connect it as an illustration for OP's question.

Winston


To be honest, I have never used that! May be I haven't come across a situation where I need them.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8418
    
  23

Yohan Weerasinghe wrote:To be honest, I have never used that! May be I haven't come across a situation where I need them.

Google for "Builder pattern". Also, there's a very good section about them in Effective Java (v2).

Very useful if you would otherwise have to provide lots of constructors, or constructors with lots of parameters.

Winston
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Winston Gutkowski wrote:
Yohan Weerasinghe wrote:To be honest, I have never used that! May be I haven't come across a situation where I need them.

Google for "Builder pattern". Also, there's a very good section about them in Effective Java (v2).

Very useful if you would otherwise have to provide lots of constructors, or constructors with lots of parameters.

Winston


Thanks a lot for the link. Gonna add bit more knowledge to my design pattern knowledge
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are public nested classes a good practice?