Java final Keyword: Difference between revisions
Jump to navigation
Jump to search
Line 8: | Line 8: | ||
An attempt to assign a final variable the second time is detected as compilation error. | An attempt to assign a final variable the second time is detected as compilation error. | ||
A ''blank final'' is a final variable that was not initialized yet. | |||
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 [[Java_7_try-with-resources|try-with-resource]] statement. |
Revision as of 20:19, 27 September 2017
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 was not initialized yet.
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.