Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

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

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

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 −

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

Tester.java:11: error: changeValue() in Tester cannot override changeValue() in FinalTester
        public void changeValue() {
                    ^
  overridden method is final
1 error

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 {
                            ^
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.
java_basic_syntax.htm
Advertisements