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



Java final keyword is used to define constant value or final methods and final classes in Java. A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants. A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.

To define a variable as a constant, we just need to add the keyword final in front of the variable declaration.

Final Variables in Java

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

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 Methods in Java

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

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 Classes in Java

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

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