Tino Intro To Java

Last modified: January 12, 2023

The "Final" Keyword

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

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; OK
  • final static boolean SOMETHING = true; OK

By convention, final variables should be ALL_CAPITALIZED with underscores between words

  • static final boolean SOMETHING = true; good
  • static final boolean SOMETHING_GREAT = true; good
  • static final boolean something = true; bad

Once a final field is initialized, it cannot be modified later.

Dark Mode

Outline