This question already has answers here
I generally prefer to put constants in the class where they belong. For example, the file constants could be in
FileManager (or something like that), where they are used. The connection constants could be in your
DBUtil class, where they are used.
Think about the JDK. Does it have a gigantic Constants class? No. The constants used by (and with)
BorderLayout are in the class
BorderLayout. The constants used by (and with)
JOptionPane are in
- You get a nice "path" to your constants.
- You might get a cross-code constants convention, if colleages follow the guidance.
- With many constants and many "domains" of constants, that class might get too cumbersome to handle.
- It's not intuitive. It reminds me more of Ant properties than java constants.
- It prevents modularization of your code. Say you want to separate your DB connecting package and File handling package to different jars which might be used by different applications. It is redundant to deliver your DB management jar with file handling constants. And you can easily get to have many such modules.
In conclusion, it's something that might be handy when you write just some little application for your own self. Generally, it is not a good practice.
Use Enum http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
each JVM will have only one copy of enum object