Final Keyword

 

In Java programming language the final keyword is used to restrict the users. In java programming language final can be a variable, method or a class. A variable can be a final variable and if this variable does not have any value then it will be called blank final variable or uninitialized final variable. A final variable can only be initialized in the constructor.

The blank final variable can be static and then it can be initialized in the static block only.

 

Java final Variable:

If any variable in the program is made final and is initialized with a value then we cannot change its value. In other words we can say that this variable acts like a constant.

 

Example of final variable:

Consider the following example in which the variable “velocity” is made final variable and is initialized with a value, we cannot change its value because when a final variable is assigned a value it can never be changed:

CODE:

class car {

  final int velocity = 50;

  void run () {

   velocity = 100;

  }

  public static void main (String args[]) {

  Car  c1 = new Car ();

  c1. run();

  }

 }

OUTPUT:

Compile time error

The compilation error occurred because we tried to change the value of the final variable that is “velocity”.

When we use the final keyword we cannot change the value of a variable, we cannot override a method and cannot implement inheritance in our program.

 

Java final method:

In java programming language if a method is made final by using the final keyword then we cannot override this method.

 

Example of final method:

Consider the following example in which the final method is declared and we will demonstrate that what will happen if we try to override the final method:

CODE:

class Vehicle {

final void display () {

system. out. println (“Vehicle is running”);

}

}

class Car extends Vehicle {

void display () {

system. out. println (“Car is running”);

}

public static void main (string args []) {

Car c1 = new Car ();

c1. display ();

}

}

OUTPUT:

Compile time error

In the above example, we got a compilation error because we tried to override the method that was declared as final in the parent class.

 

Java final class:

In java programming language we can make a class final by using the final keyword and when a class is declared as final then we cannot extend the class that is inheritance cannot be implemented.

 

Example of final class:

Consider the following in which the class vehicle is declared as final and hence we cannot extend it:

CODE:

final class Vehicle { }

class Car extends Vehicle {

void display () {

system. out. println (“Car is running safely”);

}

public static void main (string args []) {

Car c1 = new Car ();

c1. display ();

}

}

OUTPUT:

Compile time error

In the above example we got a compilation error because the class that is vehicle was declared as final. If a class is declared as final then we cannot extend the class that is we cannot use the concept of inheritance.

 

Is final method inherited?

A final method in java programming language can be inherited but the final method cannot be overridden. Consider the following example to demonstrate this:

CODE:

class Vehicle {

final void display () {

system. out. println (“Vehicle is running”);

}

}

class Car extends Vehicle {

public static void main (string args []) {

new Car (). display ();

}

}

OUTPUT:

Vehicle is running

In the above example the display method is declared as final and is also inherited in the main () method successfully.


Q. What is blank or uninitialized final variable?

A blank final variable is that which is not initialized when it is declared as final. We use the final variable to create a variable that may not be changed once it is initialized. For example if the programmer wants to create a variable and wants to initialize it at the time of object creation then when it is initialized it cannot be changed. For example the id of an employee of the company can be made a final variable so that the id of the employee cannot be changed as for each employee id is unique. Consider the following example:

CODE:

class Employee {

final int id;

string name;

}

Q. Can we initialize blank final variable?

In Java programming language a blank final variable can be initialized but it can only be initialized in constructor. Consider the following example:

class Car {

final int velocity;

Car () {

velocity = 50;

system. out. println (“Velocity =” + velocity);

}

public staic void main (string args []) {

new Car ();

}

}

OUTPUT:

Velocity = 50

In the above example, the blank final variable that is “velocity” is initialized in the constructor and its value is printed.

Static blank final variable:

A static blank final variable in java programming language is that which is declared as final and static but is not initialized or it is not assigned any value. The static blank final variable can only be initialized in the static block.

Example of static blank final variable:

Consider the following example in which the static blank final variable is initialized in the static block:

CODE:

class Employee {

static final int id;

static

{

id = 122;

}

public static void main (string args []) {

system. out. println (Employee. id);

}

}

In the above example a static final variable that is ‘id’ of the class employee is declared and is initialized in the static block. The value of this variable is then printed in the main () method by using the name of the class.

Q. What is final parameter?

A parameter of a method if declared as final then it will be called final parameter. Once a parameter is declared as final we cannot change the value of this variable.

Consider the following example in which final parameter is declared:

CODE:

class Car {

int square (final int x) {

n = n + 1;

return n * n;

}

public static void main (string args []) {

Car c1 = new Car ();

c1. square ();

}

}

OUTPUT:

Compile time error

In the above example the parameter of the method square () of class Car is declared as final. In the method square () we tried to change the value of the value of parameter by adding 1 and saving the value into itself, therefore, we got a compilation error.

Q. Can we declare a constructor final?

In java programming language we cannot declare a constructor as final because a constructor can never be inherited.