0% found this document useful (0 votes)
0 views

Java - final Keyword

The final keyword in Java is used to define constants and to make variables, methods, and classes non-changeable. A final variable can only be initialized once, a final method cannot be overridden, and a final class cannot be subclassed. It ensures that the properties of the defined elements remain unchanged throughout the program.

Uploaded by

Jithin S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Java - final Keyword

The final keyword in Java is used to define constants and to make variables, methods, and classes non-changeable. A final variable can only be initialized once, a final method cannot be overridden, and a final class cannot be subclassed. It ensures that the properties of the defined elements remain unchanged throughout the program.

Uploaded by

Jithin S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Page 1 of 4

Java - final Keyword


The final keyword is used to define a constant and to make attributes, methods, and
classes final i.e., non-changeable. Methods and classes which are defined as final cannot
be inherited and overridden. The final keyword is a non-access modifier.

The final keyword can be used with the following topics:

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.

Example of Final Variable

Open Compiler

public class Tester{


final int value = 10;

public void changeValue() {


value = 14;
}
public static void main(String[] args) {
Tester tester = new Tester();
tester.changeValue();
}
}

Output

https://www.tutorialspoint.com/java/final_keyword_in_java.htm 1/4
Page 2 of 4

Tester.java:6: error: cannot assign a value to final variable value


value = 14;
^
1 error

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.

Example of Final Method

You declare methods using the final modifier in the class declaration, as in the following
example −

Open Compiler

class FinalTester {
int value = 10;

public final void changeValue() {


value = 12;
}
}

public class Tester extends FinalTester {


public void changeValue() {
value = 14;
}
public static void main(String[] args) {
Tester tester = new Tester();
tester.changeValue();
}
}

Output

https://www.tutorialspoint.com/java/final_keyword_in_java.htm 2/4
Page 3 of 4

Tester.java:11: error: changeValue() in Tester cannot override changeValue() in FinalTes


public void changeValue() {
^
overridden method is final
1 error

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.

Example of Final Class

final class FinalTester {


int value = 10;

public void changeValue() {


value = 12;
}
}

public class Tester extends FinalTester {


public void changeValue() {
value = 14;
}
public static void main(String[] args) {
Tester tester = new Tester();
tester.changeValue();
}
}

Output

Tester.java:10: error: cannot inherit from final FinalTester


public class Tester extends FinalTester {

https://www.tutorialspoint.com/java/final_keyword_in_java.htm 3/4
Page 4 of 4

^
1 error

When to Use final Keyword in Java?


The final keyword can be used with variables, methods, and classes to make them
constant so that their value or properties cannot be changed. The final keyword can be
used for the following context:

To define a final variable so that its value should not be changed.

To define a final method so that it should not be overridden.

To define a final class so that it should not be inherited.

https://www.tutorialspoint.com/java/final_keyword_in_java.htm 4/4

You might also like