Ideal way to organize Java constants

Reg Mem picture Reg Mem · Feb 28, 2012 · Viewed 12.6k times · Source

We have a huge projects based on an old jdk 1.4. We have migrated the web app to JDK 1.6 but still lot of inefficient practices and bad design exist in the code.

On of the major pain point huge java classes 2500+ lines of code in single java file. So many files like these.

In an attempt to refactor the classes I had started off by removing constants and putting constants in different Constants.java file. but since there are so many constants through-out the application, the constants file has the risk of growing to humongous proportions.

I would appreciate feedback on what strategy are developers adopting to keep code clean and maintainable.

Answer

Matthieu Napoli picture Matthieu Napoli · Feb 28, 2012

Keep your constants in the class they are related to, don't feel obligated to extract them. It may clean up the code of the class, but mixing unrelated constants in a file is not an improvement.

Keep things related together.

And also you can convert them to Enums when possible/useful (but that can require some refactoring).