Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java naming convention for static final variables [duplicate]

there is a rule which says:

Names representing constants (final variables) must be all uppercase using underscore to separate words (taken from http://geosoft.no/development/javastyle.html)

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)?

like image 816
Aleksey Avatar asked Aug 31 '11 15:08

Aleksey


People also ask

How do you name a static variable in Java?

For variables, the Java naming convention is to always start with a lowercase letter and then capitalize the first letter of every subsequent word. Variables in Java are not allowed to contain white space, so variables made from compound words are to be written with a lower camel case syntax.

What are the naming conventions for a final variable?

2.4 Names representing constants (final variables) must be all uppercase using underscore to separate words. Common practice in the Java development community and also the naming convention used by Oracle for the Java core packages. In general, the use of such constants should be minimized.

Should static variables be capitalized Java?

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

How do you initialize a static final variable?

The only way to initialize static final variables other than the declaration statement is Static block. A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.


1 Answers

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.
like image 73
mre Avatar answered Sep 22 '22 19:09

mre