Aleksey Aleksey - 1 year ago 152
Java Question

Java naming convention for static final variables

there is a rule which says:

Names representing constants (final variables) must be all uppercase
using underscore to separate words
(taken from

that works fine for primitive types like int or strings:

private static final int MAX_COUNT = 10;

But what's about non primitive types? In most cases I've seen the following:

private static final Logger log = Logger.getLogger(MyClass.class);

or in singletons, where instance variable is not in upper case.

The question is what is the right way to declare those types of variables (like log and instance)?

mre mre
Answer Source

That's still a constant. See the JLS for more information regarding the naming convention for constants. But in reality, it's all a matter of preference.

The names of constants in interface types should be, and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with components separated by underscore "_" characters. Constant names should be descriptive and not unnecessarily abbreviated. Conventionally they may be any appropriate part of speech. Examples of names for constants include MIN_VALUE, MAX_VALUE, MIN_RADIX, and MAX_RADIX of the class Character.

A group of constants that represent alternative values of a set, or, less frequently, masking bits in an integer value, are sometimes usefully specified with a common acronym as a name prefix, as in:

interface ProcessStates {
  int PS_RUNNING = 0;
  int PS_SUSPENDED = 1;

Obscuring involving constant names is rare:

  • Constant names normally have no lowercase letters, so they will not normally obscure names of packages or types, nor will they normally shadow fields, whose names typically contain at least one lowercase letter.
  • Constant names cannot obscure method names, because they are distinguished syntactically.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download