Non-uppercase constants in Java
This question about why constants in Java are uppercase by convention made me try to think of counter example开发者_运维知识库s.
I can think of at least one (Double.NaN
). Are there others?
Of course, public final static PrintStream out
(in java.lang.System.out
). But it's a very good exception, because System.OUT.println
is just ugly.
Also, most of the time loggers are initialized as follows:
private static final Logger logger = Logger.getLogger(MyClass.class);
However, in both cases these are not constants in the true sense of the term. So perhaps we can make a distinction:
Fields that are static
because they need a static access, and final
because they should not be re-assigned at runtime, are not necessarily constants.
There are lots of serialVersionUID
!
Others in ResultSetMetaData
like columnNoNulls
, columnNullable
...
DatabaseMetaData
and ICC_Profile
have lots of mixed case constants.
Here is a list with most, if not all, JavaSE constants: Constant Field Values
Color constants like black, red, green etc from java.awt.Color class.
It should be noted that java.awt.Color also provides the uppercase alternatives (e.g. BLACK, RED, GREEN etc) of these constants.
java.util.logging.Logger.global is a constant with all lowercase
null
, true
and false
. They are arguably keywords, but when you get down to it, they're constants evaluating to 0x00, 0x01 and 0x00 respectively.
The length
of array instances.
Btw, I see examples mentioned of objects that are "constant" in one respect, namely their reference does not change - the reference is a final static, but when those objects are active; i.e. their internal attributes do change when methods on these objects are called. In those cases I would not use the UPPERCASE naming convention as the objects are no constants in my view.
精彩评论