How to implement Constants in Java [duplicate]

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 JOptionPane.



  1. You get a nice "path" to your constants.
  2. You might get a cross-code constants convention, if colleages follow the guidance.


  1. With many constants and many "domains" of constants, that class might get too cumbersome to handle.
  2. It's not intuitive. It reminds me more of Ant properties than java constants.
  3. 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

each JVM will have only one copy of enum object


I have a bunch of model objects. These objects end up being rendered as views (say forms) in a rich client app. I started to annotate the fields in the model objects (Java annotations) with things ...

I get a lot of IndexOutOfBoundsException from the any Arraylist I use. Most of the times it works fine but sometimes I get this annoying error on Arraylists i use on my project. The main cause is ...

I have multiple JREs installed (6,7,8 both 32 and 64 bit). The way I have set things up is that I have the set up a number of JAVA_x_HOME paths and then reference JAVA_HOME=%JAVA_x_HOME% depending on ...

Edit: I am using quartz-2.1.5.jar. Here's the summary of my classes: HttpPollingJob extends PollingJob extends ScheduledJob implements org.quartz.Job Specifically: 1) ScheduledJob implements ...