34

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 Sep 20, 2012 at 20:09

5 Answers 5

25

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.

2
  • 1
    Why in case of Logger we use private static final Logger log = Logger.getLogger(getClass().getName()); ? Sep 20, 2012 at 19:26
  • 2
    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
    – RNJ
    Sep 20, 2012 at 19:27
24

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
  • 2
    Can you explain what an "ANSI constant" is and how they differ from other static final constants? Oct 15, 2013 at 12:35
  • There doesn't seem to exist any Java-related definition for ANSI constants. (sources: SO, coderanch.com)
    – Brandlingo
    Feb 10, 2014 at 11:26
  • @chepner - Nice comment! As of now, the link is dead, I was able to read the relevant part thanks to you ;)
    – Davor
    May 31, 2014 at 9:38
  • 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? Dec 4, 2020 at 7:41
4

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. ;)

2
  • 4
    IMHO, it's still useful to name static constants differently than local variables. Oct 15, 2013 at 12:36
  • 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.
    – JohnyTex
    Nov 12, 2019 at 7:30
4

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.

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.

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