Java Programming Style Guide

Constant Naming Conventions


By using all upper-case for your named constants, others reading your code will immediately know that the identifier is a fixed, constant value that cannot be changed.

Using meaningful names for constants instead of using magic numbers in your code makes the code self-documenting, reducing the need for trailing comments. For example, look at the two lines of code below and determine for yourself that the use of named constants makes second line of code self-documenting and more readable.

day = (3 + numberOfDays) % 7;    //NO! uses magic numbers

final int WEDNESDAY = 3;
final int DAYS_IN_WEEK = 7;

day = (WEDNESDAY + numberOfDays) % DAYS_IN_WEEK;  //Yes, self-documenting