35

Is there any naming convention for java constant variable?
Normally we use variables with names containing uppercase letters and underscores(_).

For example:

public final class DeclareConstant {

    public static final String CONSTANT_STRING = "some constant";

    public static final int CONSTANT_INTEGER = 5;

}
1
  • 3
    it's called upper snake case, btw Commented Sep 20, 2012 at 20:09

5 Answers 5

26

Yes. That is it. It is often used for enum as well.

The only common exception is for logging where you might see

private static final Logger log = Logger.getLogger(getClass().getName());

but I prefer LOG

I often write this as UPPER_CASE, but I also write TitleCase for classes and camelCase for variables and methods.

Sign up to request clarification or add additional context in comments.

2 Comments

Why in case of Logger we use private static final Logger log = Logger.getLogger(getClass().getName()); ?
Im not sure that's true @peter-lawrey. Things File.separator in the API are lower case. I thought the convention was that if the rhs has to do something eg get a logger then it is lower case. If it is truly a constant then it is upper case
25

That is right. According to Sun:

Scroll to the bottom see constants

Constants

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)

static final int MIN_WIDTH = 4;

static final int MAX_WIDTH = 999;

static final int GET_THE_CPU = 1;

4 Comments

Can you explain what an "ANSI constant" is and how they differ from other static final constants?
There doesn't seem to exist any Java-related definition for ANSI constants. (sources: SO, coderanch.com)
@chepner - Nice comment! As of now, the link is dead, I was able to read the relevant part thanks to you ;)
How about when It comes to groups. Something like : ACTIVE_STATUS_ACTIVE, ACTIVE_STATUS_INACTIVE. I have seen some people use two Underscores to divide group name. Like this: ACTIVE_STATUS__ACTIVE, ACTIVE_STATUS__INACTIVE. Is this a standard to use?
5

variables are identifiers.

there are 3 methods to name an identifier:

  1. Camel case: used to name a function,variable

    e.g: int streamJavaMethod() or for variable, arrayBoss;

  2. Pascal case: used to name a class

    e.g: class StreamJavaClass()

  3. upper case: used to name constants

    e.g.: PIE

Although these are mere naming conventions, no strict rules are needed to follow. Professional java programmers use it.

Comments

3

Many of these naming conventions were created well before IDEs were in widespread use. These days, static constants are going to be colored differently than local variables, so the need to use a specific naming convention to identify them is greatly reduced. I would suggest that readability is a more important concern these days, and recommend using camel case instead.

To answer your question though, the other answers are right that this is the official convention. ;)

3 Comments

IMHO, it's still useful to name static constants differently than local variables.
Readability is enhanced by upper-case constants I would say. Even though modern IDE's help too of course. Some notations say you should have an "m" before member-variables, e.g. mMember as well, however, in that case where the name is actually altered I would say using IDE-coloring is enough these days.
Take into account that a good part of a developer's time is spent doing code reviews away of an IDE.
2

Yes, generally when a variable is static final it is declared with a name written in all capitals with words separated by underscores, as you have shown.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.