Tutorial :Non-uppercase constants in Java



Question:

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?


Solution:1

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


Solution:2

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.


Solution:3

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.


Solution:4

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


Solution:5

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.


Solution:6

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »