This question already has an answer here:
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)?
The language doesn't care. What's important is to follow the established styles and conventions of the project you're working on, such that other maintainers (or you five months from now) have the best possible chance of not being confused.
I think an all-uppercase name for a mutable object would certainly confuse me, even if the reference to that object happened to be stored in a
static final
variable.Well that's a very interesting question. I would divide the two constants in your question according to their type.
int MAX_COUNT
is a constant of primitive type whileLogger log
is a non-primitive type.When we are making use of a constant of a primitive types, we are mutating the constant only once in our code
public static final in MAX_COUNT = 10
and we are just accessing the value of the constant elsewherefor(int i = 0; i<MAX_COUNT; i++)
. This is the reason we are comfortable with using this convention.While in the case of non-primitive types, although, we initialize the constant in only one place
private static final Logger log = Logger.getLogger(MyClass.class);
, we are expected to mutate or call a method on this constant elsewherelog.debug("Problem")
. We guys don't like to put a dot operator after the capital characters. After all we have to put a function name after the dot operator which is surely going to be a camel-case name. That's whyLOG.debug("Problem")
would look awkward.Same is the case with
String
types. We are usually not mutating or calling a method on aString
constant in our code and that's why we use the capital naming convention for aString
type object.Don't live fanatically with the conventions that SUN have med up, do whats feel right to you and your team.
For example this is how eclipse do it, breaking the convention. Try addingimplements Serializable
and eclipse will ask to generate this line for you.Update: There were special cases that was excluded didn't know that. I however withholds to do what you and your team seems fit.
A constant reference to an object is not a constant, it's just a constant reference to an object.
private static final
is not what defines something to be a constant or not. It's just the Java way to define a constant, but it doesn't mean that everyprivate static final
declaration was put there to define a constant.When I write
private static final Logger
I'm not trying to define a constant, I'm just trying to define a reference to an object that isprivate
(that it is not accessible from other classes),static
(that it is a class level variable, no instance needed) andfinal
(that can only be assigned once). If it happens to coincide with the way Java expects you to declare a constant, well, bad luck, but it doesn't make it a constant. I don't care what the compiler, sonar, or any Java guru says. A constant value, likeMILLISECONDS_IN_A_SECOND = 1000
is one thing, and a constant reference to an object is another.Gold is known to shine, but not everything that shines is gold.
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.
These variables are constants, i.e.
private static final
whether they're named in all caps or not. The all-caps convention simply makes it more obvious that these variables are meant to be constants, but it isn't required. I've seenin lowercase before, and I'm fine with it because I know to only use the logger to log messages, but it does violate the convention. You could argue that naming it
log
is a sub-convention, I suppose. But in general, naming constants in uppercase isn't the One Right Way, but it is The Best Way.