• Post Reply Bookmark Topic Watch Topic
  • New Topic

Good design with Inner classes  RSS feed

 
Aditya Karnad
Greenhorn
Posts: 16
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a situation where I visualise a large component say X and smaller components in X, say Y and Z which in turn maybe composed of further smaller ones...

Now the simple way to design such beans would be to have instance variables of Y and Z type in X:



Was wondering if it would be good design practice to use Inner classes in this situation. Are there like general guidelines or a rule of hand as to when and when not to use Inner classes? Don't have a lotta experience using Inner classes... Would this complicate the code or cause maintenance issues?

Thanks for your help!
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like inner classes (except for some UI stuff) because most developers find them more complicated then regular classes and the inability to define static variables. I wouldn't use them. If you want to hide them from the rest of the program I would give them a package scope and put them in a special package.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Wouter. Composition can be implemented by means of having an instance variable- HAS-A relationship- The one which you have given as example. Its always better to keep the code simple. And there might be possibility that components Y and Z can exist independently.
 
Aditya Karnad
Greenhorn
Posts: 16
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,

The case I am dealing with is definitely composition and not aggregation where the inner objects can exist independently - in which case inner classes are definitely a bad idea.
I think I'll go with the instance variables approach - but just for the sake of knowing, would there be anything wrong with the design (keeping aside the complexity) if I used Inner classes? Or is that an acceptable approach too?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea of good design is how it is perceived by the developers around. So a good design vouched by few might be a bad design for few others. But there are few good practices around- but I am not aware of anything related to use of Inner classes.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!