Java final Keyword
Example
Set a variable tofinal, to prevent it from being overridden/modified:
public class Main { final int x = 10; public static void main(String[] args) { Main myObj = new Main(); myObj.x = 25; // will generate an error: cannot assign a value to afinal variable System.out.println(myObj.x); }}Definition and Usage
Thefinal keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override).
Thefinal keyword is useful when you want a variable to always store the same value, like PI (3.14159...).
Thefinal keyword is called a "modifier". You will learn more about these in theJava Modifiers Chapter.
Related Pages
Read more about attributes ourJava Class Attributes Tutorial.

