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

Are public nested classes a good practice?

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Martin & Jesper, those are both great examples! I recant.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic