Jean-Baptiste,
You can see more details is JLS:
JLS §4.12.4:
A variable of primitive type or type String, that is final
and initialized with a compile-time constant expression (§15.28),
is called a constant variable.
JLS §6.1:
Constant Names
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.
JLS §8.1.3:
Inner classes may not declare static members, unless they
are constant variables (§4.12.4), or a compile-time error occurs.
=================================================================
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestConstants {
public class InnerClass {
private static final Logger logger =
LoggerFactory.getLogger(InnerClass.class);
}
}
We got compile error, so logger object is not constant, -
it is mutable object and it must not be named as 'LOGGER'.
=================================================================
More details, JLS §8.3.2.1:
At run-time, static fields that are final and that are initialized
with constant expressions (§15.28) are initialized first (§12.4.2).
This also applies to such fields in interfaces (§9.3.1). These fields
are "constants" that will never be observed to have their default
initial values (§4.12.5), even by devious programs (§13.4.9).
=================================================================
"constant" - is well defined term in Java Language Specification.
Authors: James Gosling, Bill Joy, Guy Steele, Gilad Bracha, Alex Buckley
...for me, a private static final field is indeed a constant
What exactly term "constants" means is defined in JLS by James Gosling.
So once again IMHO, from a static analysis point of view, the rule is
correct as it is: a private static final field is a constant and should
follow the associated naming scheme.
Please, please, read exact definitions from Java Language Specification
Now, just to push the envelope on this: how would you define a rule that
detects that a mutable object (e.g java.util.HashSet<?>) is used as a
constant (final field)? How, with static analysis (or even dynamic
analysis, and eventually a bit of introspection/reflection), can you
detect that an object is immutable? Hint: this is a popular subject for
doctorate thesis.
We are talking now only about Java Programming Language
and about Code Conventions for the Java Programming Language.
In Java Code Conventions from Sun/Oracle you can read what only
constants should be named in ALL_CAPS_STYLE. *ONLY* constants.
See: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
In Java Code Conventions from Google you can read, what only
constants should be named in ALL_CAPS_STYLE. *ONLY* constants.
See:
http://google-styleguide.googlecode.com/svn/trunk/javaguide.html#s5.2.4-constant-names
Every constant is a static final field, but not all static final fields
are constants. Before choosing constant case, consider whether the field
really feels like a constant. For example, if any of that instance's
observable state can change, it is almost certainly not a constant.
Merely intending to never mutate the object is generally not enough.
Examples:
// Constants
static final int NUMBER = 5;
static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann");
static final Joiner COMMA_JOINER = Joiner.on(','); // because Joiner is
immutable
static final SomeMutableType[] EMPTY_ARRAY = {};
enum SomeEnum { ENUM_CONSTANT }
// Not constants
static String nonFinal = "non-final";
final String nonStatic = "non-static";
static final Set<String> mutableCollection = new HashSet<String>();
static final ImmutableSet<SomeMutableType> mutableElements =
ImmutableSet.of(mutable);
static final Logger logger = Logger.getLogger(MyClass.getName());
static final String[] nonEmptyArray = {"these", "can", "change"};
==========================================================================
Also you can read book Effective Java, 2nd ed. by Joshua Bloch:
The sole exception to the previous rule concerns “constant fields,”
whose names should consist of one or more uppercase words separated by
the underscore character, for example, VALUES or NEGATIVE_INFINITY. A
constant field is a static final field whose value is immutable. If a
static final field has a primitive type or an immutable reference type
(Item 15), then it is a constant field. For example, enum constants are
constant fields. If a static final field has a mutable reference type,
it can still be a constant field if the referenced object is immutable.
==========================================================================
Logger is mutable reference type and => logger in not the constant.
So, logger reference type must be named 'logger' and never 'LOGGER'.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Dear Sonar developer, please, please, fix this HUGE BUG S1312 in Sonar
and stop forcing developers to name mutable objects in ALL_CAPS_STYLE.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Rule S1312 should be
Rename the "LOGGER" logger to comply with the format "log(?:ger)?"
and must not be
Rename the "logger" logger to comply with the format "LOG(?:GER)?"
--
Best regards,
Gena