views:

265

answers:

6

This question about why constants in Java are uppercase by convention made me try to think of counter examples.

I can think of at least one (Double.NaN). Are there others?

A: 

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.

Chinmay Kanchi
In C++ perhaps, but certainly not in java. There's no `(int) null` or `(int) true`
Martin
Also: they are literals, not keywords. Just as `13` is a literal or `"foo"`.
Joachim Sauer
You are probably confused with Boolean.TRUE and Boolean.FALSE which are constants but uppercase.
Roman
No, I'm not really confused. While they _are_ literals, and they _are_ keywords (of a sort), they are also symbolic constants. `null` is a constant reference to a type `null` of which `null` is the only instance (Java Language Spec). I think the same is true of `true` and `false`.
Chinmay Kanchi
The point remains that your declaration that null/true/false "evaluate to" 0/1/0 is bizarre. There is no such association between Object references and integral values or between booleans and integral values in the Java language. Were you pitching your answer at VM engineers?
Kevin Bourrillion
They are not 'keywords of a sort'. They are literals. See the Java grammar.
EJP
+2  A: 

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.

missingfaktor
+2  A: 

java.util.logging.Logger.global is a constant with all lowercase

GK
+3  A: 

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.

Bozho
I agree; I believe that constants are always value objects (immutable ones at that, of course).
Kevin Bourrillion
+3  A: 

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

Carlos Heuberger
The link to all of the constants make this a winner!
JRL
A: 

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.

rsp