Final Keyword in Java | Java Tutorial

As a programmer it is very much essential to know the uses of final keyword in Java. final keyword can be used along with variables, methods and classes. In this article we will be looking into the following topics.

1) final variable
2) final method
3) final class

1. Java final variable

A final variable is a variable whose value cannot be changed at anytime once assigned, it remains as a constant forever. Lets look into the below code

Example of final variable

public class Travel 
{
    final int SPEED=60;
    void increaseSpeed(){  
       SPEED=70;
    }  
    public static void main(String args[])
    {  
       Travel t=new  Travel();  
       t.increaseSpeed();  
    }  
}

Output :

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The final field Travel.SPEED cannot be assigned

	at com.javainterviewpoint.Travel.increaseSpeed(Travel.java:7)
	at com.javainterviewpoint.Travel.main(Travel.java:12)

The above code will give you Compile time error, as we are tying to change the value of a final variable ‘SPEED’.

Can we have a uninitialized  final variable ?

No, we cannot have a final variable which is not initialized.

package com.javainterviewpoint;

public class Travel 
{
    final int SPEED;
    public static void main(String args[])
    {  
       Travel t=new  Travel();  
       System.out.println("Travelling Speed is :"+t.SPEED);
    }  
}

Output :

We will get a error like “The blank final field SPEED may not have been initialized”. All the final variable has to be initialized at the very beginning or you can initialize through a constructor(blank final variable) which we will see in the later part.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The blank final field SPEED may not have been initialized

	at com.javainterviewpoint.Travel.(Travel.java:3)
	at com.javainterviewpoint.Travel.main(Travel.java:8)

What is a blank final variable ?

A final variable which is not initialized at the time of declaration is known as blank final variable. We must initialize a blank final variable in the constructor else it will throw compilation error. Lets now change the above to initialize the “SPEED” variable through a constructor.

package com.javainterviewpoint;

public class Travel 
{
    final int SPEED;
    Travel()
    {
        SPEED = 60;
    }
    public static void main(String args[])
    {  
       Travel t=new  Travel();  
       System.out.println("Travelling Speed is :"+t.SPEED);
    }  
}

Output :

Travelling Speed is :60

What is the use of a blank final variable ?

Lets say we have a class Employee, consisting a field called SOCIAL_SECURITY_NUMBER. Once the employee is registered the SOCIAL_SECURITY_NUMBER cannot be changed and we cannot initialize the SOCIAL_SECURITY_NUMBER well in advance as it differs for each employee. In such cases we can use the blank final variable and initialize the final variable like below.

package com.javainterviewpoint;

public class Employee
{
    public final int SOCIAL_SECURITY_NUMBER;
    Employee(int ssn)
    {
        SOCIAL_SECURITY_NUMBER = ssn;
    }
    public static void main(String args[])
    {
        Employee e1 = new Employee(1234);
        System.out.println("Social Security Number of Emploee e1 : "+e1.SOCIAL_SECURITY_NUMBER);
        Employee e2 = new Employee(5678);
        System.out.println("Social Security Number of Emploee e2 : "+e2.SOCIAL_SECURITY_NUMBER);
    }
}

Output :

Social Security Number of Emploee e1 : 1234
Social Security Number of Emploee e2 : 5678

what is Static blank final variable?

A static final variable is a variable which is also not initialized at the time of declaration. It can be initialized only in static block.

package com.javainterviewpoint;

public class Employee
{
    public static final int SOCIAL_SECURITY_NUMBER;
    static
    {
        SOCIAL_SECURITY_NUMBER = 1234;
    }
    public static void main(String args[])
    {
        Employee e1 = new Employee();
        System.out.println("Social Security Number of Emploee e1 : "+e1.SOCIAL_SECURITY_NUMBER);
    }
}

Output :

Social Security Number of Emploee e1 : 1234

2. Java final method

When you declare a method as final, then it is called as final method. A final method cannot be overridden.

package com.javainterviewpoint;

class Parent
{
    public final void disp()
    {
        System.out.println("disp() method of parent class");
    }
}
public class Child extends Parent
{
    public void disp()
    {
        System.out.println("disp() method of child class");
    }
    public static void main(String args[])
    {
        Child c = new Child();
        c.disp();
    }
}

Output : We will get the below error as we are overriding the disp() method of the Parent class.

Exception in thread "main" java.lang.VerifyError: class com.javainterviewpoint.Child overrides final method disp.()V
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(Unknown Source)
	at java.security.SecureClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.defineClass(Unknown Source)
	at java.net.URLClassLoader.access$100(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)

Can a final method be inherited ?

Yes, the final method can be inherited but cannot be overridden.

package com.javainterviewpoint;

class Parent
{
    public final void disp()
    {
        System.out.println("disp() method of parent class");
    }
}
public class Child extends Parent
{
    public static void main(String args[])
    {
        Child c = new Child();
        c.disp();
    }
}

Output :
The above example clearly shows that the disp() method of the Parent class is inherited to the Child class.

disp() method of parent class

3. Java final class

A final class cannot be extended(cannot be subclassed), lets take a look into the below example

package com.javainterviewpoint;

final class Parent
{
}
public class Child extends Parent
{
    public static void main(String args[])
    {
        Child c = new Child();
    }
}

Output :
We will get the compile time error like “The type Child cannot subclass the final class Parent”

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

	at com.javainterviewpoint.Child.main(Child.java:8)

Leave a Reply

Your email address will not be published. Required fields are marked *