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;

}
moffeltje
  • 4,521
  • 4
  • 33
  • 57
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82

5 Answers5

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Why in case of Logger we use private static final Logger log = Logger.getLogger(getClass().getName()); ? – Shreyos Adikari Sep 20 '12 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 '12 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;

Mobile Dan
  • 6,444
  • 1
  • 44
  • 44
zw324
  • 26,764
  • 16
  • 85
  • 118
  • 2
    Can you explain what an "ANSI constant" is and how they differ from other `static final` constants? – Simon Forsberg Oct 15 '13 at 12:35
  • There doesn't seem to exist any Java-related definition for ANSI constants. (sources: [SO](http://stackoverflow.com/questions/19174748/java-ansi-constants), [coderanch.com](http://www.coderanch.com/t/386728/java/java/ANSI-Constants)) – Brandlingo Feb 10 '14 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 '14 at 09: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? – Thamidu de Harshitha Dec 04 '20 at 07:41
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.

Sarin Jacob Sunny
  • 2,138
  • 3
  • 29
  • 61
Subrat Sahu
  • 71
  • 1
  • 6
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. ;)

Eric Lindauer
  • 1,813
  • 13
  • 19
  • 4
    IMHO, it's still useful to name static constants differently than local variables. – Simon Forsberg Oct 15 '13 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 '19 at 07:30
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.

arshajii
  • 127,459
  • 24
  • 238
  • 287