What is the final keyword in Java?

Used onMeaning
VariableThe value can be set only once (a constant)
MethodCan not be overridden in a subclass
ClassCan not be extended (for example String is final)
ParameterCan not be changed inside the method
public class Final {
    public static void main(String[] args) {
        final int max = 10;
        max = 20;
    }
}
Output

Final.java:4: error: cannot assign a value to final variable max
max = 20;
^
1 error
error: compilation failed

A final reference can not point to another object, but the object itself can still change. See also final, finally and finalize.


Learn more about the similar topics:
Tutorials
Java Classes and Objects
Java Constructors
Java static and this Keywords
Java Encapsulation and Access Modifiers
Java Inheritance
Java Polymorphism
Java Abstract Classes
Exercises & Assignments
Build a BankAccount class
Calculate areas with inheritance and polymorphism
Use a sealed interface and pattern matching
Interview Questions & Answers
What is a class and an object in Java?
What is a constructor in Java?
What is the difference between a constructor and a method in Java?
What is the this keyword in Java?
What is the static keyword in Java?
What is the final keyword in Java?
What are the four pillars of OOP in Java?