This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Good design with Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Good design with Inner classes" Watch "Good design with Inner classes" New topic
Author

Good design with Inner classes

Aditya Karnad
Greenhorn

Joined: Feb 20, 2007
Posts: 10

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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2928
    
  15

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.


Mohamed Sanaulla | My Blog
Aditya Karnad
Greenhorn

Joined: Feb 20, 2007
Posts: 10

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

Joined: Sep 08, 2007
Posts: 2928
    
  15

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Good design with Inner classes
 
Similar Threads
modifier tutorial
WHy does main() work with private accessibility?
Inner Classes
Multi-dimension arrays
+ - operation