final keyword in java

Posted on

final keyword

final keyword is used for restrictions. It is used with variable to restrict its re assignment, it is used with method to restrict its overriding and it is used with class  to restrict it from inheritance.

The final keyword is used with :

  • variable
  • method
  • class

Final variable : when we declare any variable final then the value of the variable become constant e. We can not change the value of the variable once initialized. We can make any variable as final variable. The final variable can be initialized at the time  of declaration. An  uninitialized  final variable or a final variable that has no value is called blank final variable or uninitialized final variable. A blank final variable can be initialized in a constructor only. If we try to reinitialize any variable we will get compile time error saying “can not assign a value to final variable ”.

 

E.g  1:

public class FinalVariable {

final String name =”nandini”;

void display()

{

name=”priyanka”;

System.out.println(name);

}

public static void main(String[] args)

{

FinalVariable f = new FinalVariable();

f.display();

}

}

————————————————————————————————————————————— 

Output : The final field Finalvar.name cannot be assigned

—————————————————————————————————————————————-

  • A static variable can also be declared as the final variable. A blank static final variable can be initialized in static block only.

  E.g :

public class FinalVariable {

final static String name;

static{

name=”nandini”;

}

public static void main(String[] args)

{

System.out.println(name);

}

}

 

—————————————————————————————————————————————–

Output : nandini

—————————————————————————————————————————————–

 

final method : A method is declared as final to restrict the method over ridding i.e. a parent class method can not be redefined in the child class, the child class has to follow the parent class method implementation only throughout the whole program execution.

   E.g. :

class Parent {

 

final void display()

{

System.out.println(“displaying parent class method”);

}

}

class Child extends Parent

{

void display()

{

System.out.println(“displaying child class method”);

}

public static void main(String[] args)

{

Child c = new Child();

c.display();

}

}

 

—————————————————————————————————————————————-

Output : compile time error : cannot override final method from P

—————————————————————————————————————————————-

 

final class : A class is declared as final to restrict its inheritance i.e. a child class can not extend a final parent class.   

         E.g.

            final class Parent {

final void display()

{

System.out.println(“displaying parent class method”);

}

}

class Child extends Parent

{

void display()

{

System.out.println(“displaying child class method”);

}

public static void main(String[] args)

{

Child c = new Child();

c.display();

}

}

—————————————————————————————————————————————-

Output : compile time error

—————————————————————————————————————————————-