final (Java)

final marks something as unchangeable. A final variable can't be reassigned after it's set, a final method can't be overridden, and a final class can't be extended.

Java

final is most often used to create constants — values that should never change, conventionally named in ALL_CAPS. Trying to reassign a final variable causes a compile error.

Combined with static, it creates a class-level constant like public static final double PI = 3.14159.

Examples

Java

final int MAX = 100;
System.out.println(MAX);
// MAX = 200;  // would not compile

Output:

100

Learn it in a lesson

See also

Back to the Dictionary