Take the 2-minute tour ×
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.

share|improve this question

migrated from stackoverflow.com Mar 18 '11 at 20:18

This question came from our site for professional and enthusiast programmers.

#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

8 Answers 8

up vote 19 down vote accepted

I'll only answer for Java:

I consider it good practice to use constants for readability of code as well as when a value is used at multiple places.

private static final int SECONDS_IN_DAY = 60 * 60 * 24 // sure, this will never change, but it will make the code where you use it a lot more readable
public static final String DEFAULT_USERNAME = "JohnDoe" // this can be used in several places

I consider it bad practice to go cargo-culting and replacing every literal by a constant

private static final int SMALLEST_POSSIBLE_INCREMENTOR = 1 // don't do this; please!

Also, use names for your constants that indicate what they do, not how they do it

private static final int SEVEN = 7 // oh please please don't do this

As for where to put them; I think constants belong in the class they belong to. For instance, if you have a datehelper class, that would be a good place SECONDS_IN_DAY. If you have a User class, that's where to put the DEFAULT_USER_NAME.

Only put them in a global constants class if there's really no logical place to put them; which usually means your domain model is not the best it can be. Don't be too rough on yourself though.

share|improve this answer

In C++, I much prefer this:

const int FOO = 42;

over this:

#define FOO 42

Not only is it much safer to use, but it also means you can't get weird errors if you use FOO in the wrong place.

share|improve this answer
Shouldn't there be a const int singleton that creates a 42 factory to supply the 42 when needed? –  Martin Beckett Mar 18 '11 at 22:16
@Martin Beckett: Only in Java. –  Jerry Coffin Mar 19 '11 at 0:19
@MartinBeckett Only if the 42 factory is abstract and the actual implementation is injected via Spring, so you can change it in case the mathematical definition of 42 changes. –  Andres F. Apr 23 '12 at 19:29
@AndresF. "the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change." Xerox Fortran manual –  Martin Beckett Apr 23 '12 at 20:51

For java

Do you use dedicated headers (C++) or classes (Java) for all constants?

Yes , we dedicate a class, having

public, static, final fields.

Do you turn all literal values into constants or just those you want to use in multiple places?

only those , which are really constants.

Also see:

share|improve this answer

It is generally agreed to be good practice to always define a variable for any constant you want to use. There are some trivial exceptions, such as if you want the reciprocal of a value you don't need to make the 1 a variable when you divide by it. But anything else should be. Obviously this is essential when when you use the value in more than one place, but even if it isn't defining a variable with the value gives you the opportunity to document it, say where the value comes from, and makes it easy for someone who wants to change it later to find it in the code. For more information read Steve McConnel's book "Code Complete".

In C++ the best way to define a constant is like this.

const int FOO = 42;

In Java you want

public static final int FOO = 42;

In both cases the best place to define it depends on where it is used. If it is used only within one class, or is closely associated with one class (such as only being used when that class is used) define it in that class. If it is more widely used then you should probably make a special class (or interface or whatever) that defines constants. Don't make one for all the constants in the system - divide them up by package or namespace or whatever.

share|improve this answer
But perhaps not name the constant FOO, because some stupid guy might add a #define for that. :-) –  Bo Persson Mar 18 '11 at 16:41

I would not dare to say I have "best practices", but here are mine:

  • If only one class use the constant internally, i create a private static final ... field inside this class.
  • I try to replace all literals with constants, even if they are used only once, because it makes it easier to change the value of the constant and to understand its meaning afterwards.
share|improve this answer

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: http://www.javapractices.com/topic/TopicAction.do?Id=32

share|improve this answer

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.

share|improve this answer
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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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