Constants, final & Naming Conventions
Constants, final & Naming Conventions
Every program has values that should never change once they are set — the number of hours in a day, a tax rate, a maximum retry count. Java gives you the final keyword to lock a variable so its value cannot be reassigned. Combine final with the static modifier and you get a true class-level constant. On top of that, the Java community follows a strict set of naming conventions that tell every reader instantly what kind of identifier they are looking at. Both topics are short rules with outsized impact on code quality.
The final Keyword
Marking a local variable or a field as final means it can be assigned exactly once. Any attempt to reassign it is a compile-time error, so the compiler catches the mistake before the program ever runs.
final, the reference cannot point to a different object — but the object itself can still be modified. For example, a final List<String> cannot be reassigned to a new list, but you can still call add() on it.
Class-Level Constants with static final
A final local variable is only useful inside one method. To share a constant across an entire program, declare it as public static final at the class level. The static part means it belongs to the class itself, not to any instance, so it is created once and reused everywhere.
Callers refer to a public constant as ClassName.CONSTANT_NAME, or just CONSTANT_NAME from within the same class.
Naming Constants: UPPER_SNAKE_CASE
The Java convention is to write static final constants in UPPER_SNAKE_CASE — all capital letters with words separated by underscores. This is a visual signal that a value is fixed and should not be changed.
MAX_CONNECTIONS— goodTAX_RATE— gooddefaultTimeout— bad (looks like a regular variable)MAXCONNECTIONS— bad (hard to read without separators)
static final constant. Reading MAX_LOGIN_ATTEMPTS is far clearer than reading the bare number 5 scattered through your code.
Java Naming Conventions Overview
The Java community uses a single consistent naming system. Learn these rules once and your code will look professional to every Java developer who reads it.
Classes and Interfaces — PascalCase
Every word starts with a capital letter, no separators. Nouns for classes, adjectives for interfaces.
Methods and Variables — camelCase
Start with a lowercase letter; every subsequent word starts with a capital. Methods are verb phrases; variables are noun phrases.
Packages — all lowercase
Package names are fully lowercase, typically reversed domain names with dots separating segments. No camelCase, no underscores.
Constants — UPPER_SNAKE_CASE
As shown earlier, static final fields use all-caps with underscores. This is the only case in Java where underscores appear in names.
Putting It All Together
The following class uses every convention correctly:
Max_Discount or maxDiscount for a constant, or shopping_cart for a class name, signals to every Java reader that something is wrong. Java IDEs and static-analysis tools (like Checkstyle) flag convention violations automatically — follow them from the start.
Quick Reference
- Classes / Interfaces —
PascalCase—BankAccount,Serializable - Methods / Variables —
camelCase—calculateTax(),totalAmount - Constants (static final) —
UPPER_SNAKE_CASE—MAX_SIZE,PI - Packages —
all.lowercase.dots—com.example.util
Summary
Use final to prevent reassignment of a variable and signal intent to future readers. Use public static final to create a shared class-level constant. Name those constants in UPPER_SNAKE_CASE, name classes in PascalCase, and name everything else in camelCase. These conventions are not optional style preferences — they are the standard every Java codebase follows, and breaking them is a clear sign to maintainers that something does not belong.