Last modified: January 12, 2023
The final
keyword is used to make a field unmodifiable. It also can be used on classes and methods, but we will save the discussion for later.
Final fields (also called constants) are declared by adding a final
keyword in front.
The final
keyword can be applied to any variable, regardless of whether it is an instance variable or a class variable
When both the final
and static
keywords are used, their order does not matter
static final boolean SOMETHING = true;
OKfinal static boolean SOMETHING = true;
OKBy convention, final variables should be ALL_CAPITALIZED
with underscores between words
static final boolean SOMETHING = true;
goodstatic final boolean SOMETHING_GREAT = true;
goodstatic final boolean something = true;
badOnce a final field is initialized, it cannot be modified later.