Final Keyword In Java

The final keyword in Java is used to impose limitations on the user. The use of the final keyword in Java has a wide range of applications. Final possibilities include

  • Variable
  • Method
  • Class

An empty final variable can be used with the final keyword. The term “final variable” can also refer to an uninitialized or “blank” final variable. You can only set its value in the constructor. Additionally, the final blank variable can be static, in which case it will only be initialized in the static block. We’ll be able to study these in great depth. Okay, first things first: let’s get to grips with the fundamentals of the final keyword. Join the java training in Chennai to master the final keyword in the java concept.

1Java Final Variable

Once a variable has been marked as a final keyword, its value can never be modified (It will remain the final value and is constant).

1.1A Final Variable Example

here is a final variable called speed limit, and although we intend to alter the value of this variable, we are unable to do so due to the fact that a final variable can never be altered after it has been assigned a new value.

Also, this means you need to set the value of a final variable. If the final variable is a reference, it cannot be re-bound to a different object, but the internal state of the object represented by that reference variable can be modified; for example, elements can be added or removed from the final array or collection. It is recommended that final variables be represented in all capital letters with an underscore between each word.

1.1Illustration: Final Keyword with a Variable

class Bike1{ 

 final int speedlimit=60;//final variable

 void run(){ 

 speedlimit=320; 

 } 

 public static void main(String args[]){ 

 Bike1 obj=new  Bike1();

 obj.run(); 

 }

}//end of class 

Output: Compile Time Error

Output: Compile Time Error

Learn everything about Java extensively including Final Keyword in Java by joining Java training in Chennai.

2Java final method

When the final keyword is used to declare a method, the method is referred to as a final method. A final method cannot have its behavior changed in any way. This is accomplished by the Object class, which has a number of methods that are considered final. We are required to define methods with the final keyword, and when we do so, we are obligated to implement those methods in the same way across all of the derived classes.

2.1Illustration: Final keyword with a Method

class Car

{

final void run(){System.out.println(“driving”);}

}

class Maruti extends Car

{

void run()

{

System.out.println(“driving safely with 70kmph”);

}

public static void main(String args[])

{

Maruti maruti= new Maruti();

maruti.run();

}

}

Output: Compile Time Error

3Java final class

Any class that has been declared with the final keyword cannot be extended or inherited.

Illustration: Final Keyword with Class

final class Heater{}

class Heater1 extends Heater{

void run(){System.out.println(“Heating at 45 degree”);}

public static void main(String args[]){

Heater1 heater= new Heater1();

heater.run();

}

}

Output: Compile Time Error

3.1Is the final method inherited?

Yes, final methods are inherited, but they cannot be overridden.

class Cycle{

 final void run(){System.out.println(“running…”);}

}

class Cycle1 extends Cycle{

 public static void main(String args[]){

  new Cycle1().run();

 }

}

Output: Running…

Become a master in Java concepts by joining Java training in Chennai.

3.2What does the term “blank” or “uninitialized final variable” refer to?

The term “blank final variable” refers to a final variable that has not been initialized when it is declared.

It is helpful to establish a variable that is initialized at object creation and cannot be altered after initialization. For instance, a worker’s PAN card number, and a citizen’s Aadhar number.

It shall be initialized only in the constructor

3.3Illustration of blank final variable

class Citizen{

int id;

String name;

final String AADHAR_CARD_NUMBER;

… 

}

3.4Could a blank final variable be initialized?

It is true, but only in the constructor sense.

Illustration

class Car100{

final int speedlimit;//blank final variable  

 Car100(){  

 speedlimit=120;  

 System.out.println(speedlimit); 

 public static void main(String args[]){ 

 new Car100();  

 } 

}  

Output:120

3.5Illustration of static blank final variable

class X{  

 static final int data;//static blank final variable  

 static{ data=70;}  

public static void main(String args[]){  

System.out.println(A.data);  

}  

}  

Output: Compile time error

4In other words, what is the final parameter?

Any parameter whose type is “final” cannot have its value modified and is known as a final variable.

5It is possible to make a constructor final?

The constructor is never passed on through

Learn a Java course in Chennai and become a Java specialist.