File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Nested class declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Nested class declaration" Watch "Nested class declaration" New topic

Nested class declaration

Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
In the past, I have created nested classes to keep some constant values close to the classes where they are used, similar to the following example. (I don't want the class to be able to be subclassed.)

I've been thinking lately that the following may be a better declaration because it cannot be subclassed and the compiler may be able to inline the referenced values in the code thereby providing a slight performance improvement. Would you agree?

[ January 30, 2006: Message edited by: Jay Damon ]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Both of your examples would allow the compiler to inline the constants.

And frankly, I don't understand why you are using nested classes for this at all. Why don't you just declare the constants in the top level class?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282

Thanks for your reply.

The basic answer to your question is that I use nested classes to allow me to better organize my classes. For example, a Table class is used to enumerate database table column names. This is useful because:

1) Our legacy tables may have 100s of columns. By embedding the column constants in a nested class, I can collapse the nested class to prevent it from overflowing my Outline view in Eclipse. Additionally, I maintain the constants in the order they appear in the corresponding table so I can easily see what exists and what does not in the class. And, by maintaining the constants in a nested class, I can still sort and view them in alpha order if I choose to do so.

2) It enables me to provide better documentation for our developers who may not know how to retrieve a specific table column value from our object model. We have one central class named Database that maintains an HTML table that is available via the Javadoc for that class. A developer can use Ctrl-F to find a specific database table and the corresponding classes for that table on that page. One of those classes is a Table class. By clicking on that link, a developer can easily see what is and what is not available in the corresponding table class. A developer can also use Ctrl-F to find a specific column on the Table class page which, coincidentally, also contains links the corresponding getter/setter methods in the class.

[ January 31, 2006: Message edited by: Jay Damon ]
I agree. Here's the link:
subject: Nested class declaration
It's not a secret anymore!