Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

How do you guys handle constants, especially in Java (static final) and C++ (define)?

  • Do you use dedicated headers (C++) or classes (Java) for all constants?
  • Do you turn all literal values into constants or just those you want to use in multiple places?

This is probably a bit subjective, so I'm especially interested in the reasons for your approach.


migration rejected from Sep 9 at 19:33

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as too broad by MichaelT, Snowman, gnat, Ixrec, GlenH7 Sep 9 at 19:33

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

#define isn't the best way of dealing with constants in C++. –  GWW Mar 18 '11 at 16:00
Have you read Effective C++ 3rd edition by Scott Meyers? it contains very good explication to a majority of subjective choices. –  Aditya P Mar 19 '11 at 3:11
possible duplicate of Where should you put constants and why? –  Caleb Apr 23 '12 at 21:22

3 Answers 3

Adding to @Jigar Joshi and @Joeri Hendrickx, one of the worst things you can do is put constants in an interface. Not only does this pollute the Javadoc and field list, it also just looks bad.

This is what I'm talking about

interface BadPractice {
    static final String FOO = "bar";

More information:


My rule is if I reference a literal more than 2 or 3 times, it should be a constant. Exceptions usually are 1, 0, and values that are generated by a code-generator (such as the VS Forms Desginer).

As for where I put them... Only as much scope as is needed so all other parts of code can reference them. Somtimes that has meant separate files that act as "constant collections", sometimes private scope within a class. It varies on what the situation calls for.

I figure that if I'm going to have to comment the constant, I'll name it (possibly instead of the comment). –  David Thornley Mar 24 '11 at 15:03

If I have constants that are used across many classes in an application, then it makes sense to declare those as constants in a constant class as static fields. However, Interfaces are a strict no no.

If you have few constants that you are planing only inside a class, it is better to declare those inside the same class.

I have seen people declare their String constants from across different modules inside one class a public static final String constants. What this does is, it eats up a lot of JVM memory. Since these constants are declared as static, they remain in memory for a long time until the class is finally destroyed which is just before the application exits.


Not the answer you're looking for? Browse other questions tagged or ask your own question.