• What is the Use of final class in Java?
  • What is the use of final keyword in Java?

Final Keyword In Java

The final is a keyword. This is similar to const keyword in other languages. This keyword may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program.

The final keyword in java is used to restrict the user. The final keyword can be used in many context. Final can be:


  • Variable
  • Method
  • Class
final keyword

The final keyword can be applied with the variables, that have no value it is called blank final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.

final variable

If you specify any variable as final, you can not change its value if once assigned. It will be constant.

Example to understand final variable
 class Counter
{

public static void main(String ar[])
{
	final int count=1;//specify count as final

count=count+1;//will give error, can not be changed value of count

System.out.println(count);
}
}
Output :

Counter.java:6: error: cannot assign a value to final variable count
count = count + 1;  //will give error, can not be changed value of count
 Compile Time Error:
 

Here compile time error occured in output, because final count variable's value can not be changed. and we tried to change it.


final method

If you specify any method as final, you can not override it.

Example to understand final method
 class Vehicle {
 
	final void run()// specified run() as final
	{
		System.out.println("vehicle running");
	}
}

class Car extends Vehicle {
	void run()// trying to override final run() method
	{
		System.out.println("Car running");

	}

	public static void main(String args[])

	{
		Car c = new Car();

		c.run();

	}

}
 Output:Car.java:11: error: run() in Car cannot override run() in Vehicle
    void run()  //trying to override final run() method
         ^
  overridden method is final
 

In the above program, compile time error occured because final run() of Vehicle class can not be overridden. and we tried to override it.

final class

If you specify any class as final, you can not inherit/extend it.

Example to understand final class

 final class Vehicle// specified Vehicle as final class
{
}

class Car extends Vehicle// trying to extend Vehicle class
{
	void run()

	{

		System.out.println("Car running");

	}

	public static void main(String args[]) {

		Car c = new Car();

		c.run();

	}

}
					 
					 Output:Car.java:5: error: cannot inherit from final Vehicle
class Car extends Vehicle   //trying to extend Vehicle class
					 

Question :

1. What is blank or uninitialized final variable?

A final variable which is not initialized at the declaration time, is known as blank final variable. If you do not initialize final Instance variable at declaration time, then you will have only one option to initialize it in Constructor. If you do not initialize final static variableat declaration time, then you will have only one option to initialize it in static block.

class Test

{

	static final int s;// did not initialize final static variable s

	final int i;// did not initialize final instance variable i
	static {

		s = 5;// now s can be initialized in only static block
	}

	public Test()

	{

		i = 10;// now i can be initialized in only constructor
	}

	public static void main(Strin gar[])

	{

		Test t = new Test();

		System.out.println("s = " + t.s);// can write also Test.s except of t.s

		System.out.println("i = " + t.i);

	}

}
Output :

s = 5
i = 10 

2. Is final method inherited?

Yes, final method is inherited, only you can not override it.


3. Can we declare a constructor as final?

No, constructor is not inherited so no any need to make it final.