Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – The final keyword

When we want to prevent variable, method & class from getting changed, we should declare them as final.
Java uses final keyword to declare variable, method & class as final.

Now let’s see how final keyword works when it is applied with variable, method & class.

1) Using final keyword with variable

For better understanding, first let’s see the behavior of a variable without final keyword.

Example

class WithoutFinalDemo 
{
    int NUMBER1 = 10;
	
    public void change()
    {
        NUMBER1 = 50;
    }
	
    public static void main(String args[])
    {
        WithoutFinalDemo f1 = new WithoutFinalDemo();
        System.out.println("f1.NUMBER1 :: "+f1.NUMBER1);
        f1.change();
        System.out.println("After change");
        System.out.println("f1.NUMBER1 :: "+f1.NUMBER1);		
    }
}

Output

f1.NUMBER1 :: 10
After change
f1.NUMBER1 :: 50

We can see that instance variable NUMBER1 is not declared as final. Therefore it can be re-assigned into the change() method.

Now let’s see the behavior of a variable with final keyword.

Example

class FinalDemo 
{ 
    final int NUMBER1 = 10; // variable declared as final 
	
    public void change()
    {
        NUMBER1 = 50;  // compile time error
    }
	
    public static void main(String args[])
    {
        FinalDemo f1 = new FinalDemo();
        System.out.println("f1.NUMBER1 :: "+f1.NUMBER1);
        f1.change();
        System.out.println("After change");
        System.out.println("f1.NUMBER1 :: "+f1.NUMBER1);
    }
}

This program gives compile time error saying,
                                  The final field FinalDemo.NUMBER1 cannot be assigned

This happened because the instance variable NUMBER1 is declared as final.

In the above program we have initialized the final variable at the time of declaration.
The final variable can also be initialized by using the constructor.

Example

class InitFinal 
{
    final int NUMBER1;
	
    InitFinal(int x)
    {
        NUMBER1 = x;   // final variable initialized in constructor
    }
	
    public static void main(String args[])
    {
        InitFinal f = new InitFinal(30);
        System.out.println("f.NUMBER1 :: "+f.NUMBER1);
    }
}

Output

f.NUMBER1 :: 30

2) Using final keyword with method

In Java, if any method is declared as final, it is not allowed to override.

Example

class X 
{
    public final void display() 
    {
        System.out.println("Final method");
    }
}

class Y extends X
{
    public void display()
    {
        System.out.println("overridden");
    }

    public static void main(String args[])
    {
        X x = new X();
        x.display();
    }
}

If we try to compile this program, we’ll get compile time error,
                                             Cannot override the final method from X

2) Using final keyword with Class

In Java, if any class is declared as final, it cannot be inherited by another class.

Example

final class X 
{

}

class Y extends X
{

}

If we try to compile this program, we’ll get compile time error,
                                             The type Y cannot subclass the final class X

 

Leave a reply