final Keyword -


It is used to finalize something that we cannot change. final can be classes, methods and variables.

final variable -

If we declare any variable as final then once the value is assigned to it, we cannot change it.

Example -

class Test
{
    private int a;
    private final int b, c=300;
    
    public Test()
    {
        a = 100;
        b = 200;
      //c = 300;  Error, cannot assign a value to final variable c.
    }
    
    public Test(int x, int y)
    {
        a = x;
        b = y;
    }
    
    public void setA(int x)
    {
        a = x;
    }
    public void setB(int y)
    {
       // b = y;    Error, cannot assign a value to final variable c.
    }
    
    public void disp()
    {
        System.out.println(a+"  "+b+"  "+c);
    }
    
}
     
        
public class Call {

    public static void main(String[] args) {
    
        Test t1 = new Test();
        t1.disp();
        Test t2 = new Test(10, 20);
        t2.disp();
        
    } 
}

    

Output -

        100    200     300
        10      20       300
    
Here we have declared 3 variables a, b and c, out of them b and c are final.
Variable c has assigned a value 300, so we cannot change it, that is why we are also not able to initialize it in any constructor and simple method.
Variable b is not initialized to any value so we can initialize them in constructors.
Note that we cannot initialize final variables in simple methods. Because only constructor gives guarantee that it will run only once for any object whereas methods can run any number of times.
So methods can change the value of variables for any objects and that is not possible for final variables.

final methods -

Once a method is declared as final, we cannot override it. That is we cannot change its definition.

Example -

 class Test1
{
  public void disp()
  {
      System.out.println("Test1 Disp");
  }
    
  public final void show()  // or final public void show()
  {
      System.out.println("Final Method, So cannot change definition");
  }
}
     
class Test2 extends Test1
{
    public void disp()
    {
        System.out.println("Test2 Disp");
    }
    
  /*  public void show()             We cannot override final methods
    {
        System.out.println("No way");
    }
*/
}
        
public class Call {

    public static void main(String[] args) {

        Test2  t1 = new Test2();
        t1.disp();
        t1.show();
      
    } 
} 

 

Output -

     Test2 Disp
     Final Method, So cannot change definition
 


final class -

We cannot override final classes.

Example -

final class Test1
{
}
     
class Test2 extends Test1      //Error, cannot inherit final classes
{
} 
 
Note - abstract and final are reverse of each other i.e. abstract need to be override and final cannot be override. So we cannot use abstract and final at the same time.

final reference -

Like final variables we can also declare final reference. Once a reference is declared as final, we cannot change that.

Example -

class Employee
{
    int roll;
    String name;
    
    public Employee(int r, String n)
    {
        roll = r;
        name= n;
    }
    
    public void disp()
    {
        System.out.println(roll+ "   "+ name);
    }
}
public class Call {

    public static void main(String[] args) {

        final Employee e1 = new Employee(1, "Tom");
        e1.disp();
        Employee e2 = new Employee(2, "John");
        e2.disp();
      //  e1 = new Employee(3, "Jack");     Error cannot assign value to a final reference
        e2 = new Employee(4, "Tim");
        e2.disp();
        } 
}

 
Here we have declared two references e1 and e2. e1 is final, so once any value assigned to e1, it cannot be changed.

Because e1 is final so we cannot change its value. So it cannot refer to other objects. If reference is not final then it can refer to any object as shown below -

Here firstly e2 will refer to object 3 but later we change it to object 4.