This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested class declaration

 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja,

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.

Jay
[ January 31, 2006: Message edited by: Jay Damon ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic