Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: formatting + added code sample to exception

...

The final keyword in Java is used to declare constants. Its effect is to render the affected non-composite variable immutable. Attempts to change the value of a final-qualified variable after it has been initialized result in a compile-time error. Because constants cannot be changed, it is desirable to define only one instance of them for the class; consequently, constants should also be declared with the static modifier. (See guideline DCL04-J. Declare mathematical constants as static and finalDo not apply final to public constants whose value might change in the future.)

The following code fragment demonstrates the use of static and final to create a constant:

...