Java final Keyword

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

A final variable may only be assigned to once. The final variable may be defined unassigned and immediately assigned to ("definite assignment"). Once a final variable has been assigned, it always contains the same value. If the variable contain a reference of an object, the state of the referred object may change, but the variable will always refer to the same object.

An attempt to assign a final variable the second time is detected as compilation error.

A blank final is a final variable that does not have an initializer.

A constant variable is a final variable of a primitive type or String initialized with a constant expression.

Variables are implicitly final in the following cases:

  • a field of an interface
  • a local variable declared as a resource of a try-with-resource statement.
  • an exception parameter of a multi-catch clause.