Java - final Keyword
Java - final Keyword
Variables
Methods
Classes
Final Variable
A final variable can be explicitly initialized only once. A reference variable declared final
can never be reassigned to refer to an different object.
However, the data within the object can be changed. So, the state of the object can be
changed but not the reference.
With variables, the final modifier often is used with static to make the constant a class
variable.
Open Compiler
Output
https://www.tutorialspoint.com/java/final_keyword_in_java.htm 1/4
Page 2 of 4
Final Method
A final method cannot be overridden by any subclasses. As mentioned previously, the
final modifier prevents a method from being modified in a subclass.
The main intention of making a method final would be that the content of the method
should not be changed by any outsider.
You declare methods using the final modifier in the class declaration, as in the following
example −
Open Compiler
class FinalTester {
int value = 10;
Output
https://www.tutorialspoint.com/java/final_keyword_in_java.htm 2/4
Page 3 of 4
Learn Java in-depth with real-world projects through our Java certification course.
Enroll and become a certified expert to boost your career.
Final Class
The main purpose of using a class being declared as final is to prevent the class from
being subclassed. If a class is marked as final then no class can inherit any feature from
the final class.
Output
https://www.tutorialspoint.com/java/final_keyword_in_java.htm 3/4
Page 4 of 4
^
1 error
https://www.tutorialspoint.com/java/final_keyword_in_java.htm 4/4