Final keyword in java with example

In this post, we will see about final keyword in java. Final keyword can be associated with:

Final is often used when you want restrict others from doing any changes.

[toc]

Let’s go through each by one.


Final variable

If you make any variable final then you are not allowed to change its value later.It will be constant.If you try to change value, then compiler will give you error.
Let’s take a simple example:
package org.arpit.java2blog;

public class FinalExample{

    final int count=10;

    public void setCount()
    {
        count=20;
    }
}
In above example, you will get compilation error with text “The final field FinalExampleMain.count cannot be assigned” at highlighted row.

Blank final variable

Blank final variable is the variable which is not initialised at the time of declaration. It can be initialised only in constructor.
But if you do not initialise final variable, you will get compilation error as below.
package org.arpit.java2blog;

public class FinalExample {

    final int count;

}

In above example, you will get compilation error with text “The blank final field count may not have been initialised” at highlighted row.
You can initialize final variable once in a constructor as below.

package org.arpit.java2blog;

public class FinalExample {

    final int count;

    FinalExample(int count)
    {
        this.count=count;
    }   
        public static void main(String args[])
    {
        FinalExample fe=new FinalExample(10);
        System.out.println(fe.count);   
    }
}

Above code will work fine. You might be thinking what may be use of it.
Let’s say you have Employee class and it has an attribute called empNo. Once object is created, you don’t want to change empNo.
So you can declare it final and initialize it in constructor.

static blank final variable

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

package org.arpit.java2blog;

public class FinalExample {

    static final int count;

    static   
    {
             count=10; 
    }   
    public static void main(String args[])
    {
         System.out.println(FinalExample.count);    
    }
}

When you run above program, you will get below output:

10

Final method

You can not override final methods in subclasses. You can call parent’s class final method using subclass’s object but you can not override it.

package org.arpit.java2blog;

public class Shape{

    public final void draw()
    {
        System.out.println("Draw method in shape class");
    }
}

class Rectangle extends Shape
{
    public void draw()
    {
        System.out.println("Draw method in shape class");
    }

    public static void main(String args[])
    {
        Rectangle rectangle= new Rectangle();
        rectangle.draw();
    }
}

You will get compilation error at highlighted row with text "Cannot override the final method from Shape".

If you remove draw method from rectangle class, it will work fine.

package org.arpit.java2blog;

public class Shape{

    public final void draw()
    {
        System.out.println("Draw method in shape class");
    }
}

class Rectangle extends Shape
{
    public static void main(String args[])
    {
        Rectangle rectangle= new Rectangle();
        rectangle.draw();
    }
}

When you run above program, you will get below output:

Draw method in shape class

Final class

If you declare a class final, no other class can extend it.

package org.arpit.java2blog;

final class Shape{

    public final void draw()
    {
        System.out.println("Draw method in shape class");
    }
}

class Rectangle extends Shape
{   
    public static void main(String args[])
    {
        Rectangle rectangle= new Rectangle();
        rectangle.draw();
    }
}

You will get compilation error at highlighted row with text "The type Rectangle cannot subclass the final class Shape".


Summary for final keyword

  1. You can use final keyword with variable, method, and class.
  2. You can not use final keyword with constructor.
  3. If you do not initialize final variable(unless assigned in constructor), you will get compilation error.
  4. You can not change value of final variable once initialized.
  5. You can not override final methods in the sub class.
  6. You can not extend any final class.

You may also like:
Static keyword in java with examples

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.