Java

Final Keyword In Java

This lesson will teach us how to use the final keyword. Variables, methods, and classes may all be used with the final keyword. The subjects below will be covered in depth.

1) final variable
2) final method
3) final class

1) final variable

The only variables left are constants. Once a final variable has been initialized, it cannot be changed. Let’s look at the code below:

class Demo{  

   final int MAX_VALUE=99;
   void myMethod(){  
      MAX_VALUE=101;
   }  
   public static void main(String args[]){  
      Demo obj=new  Demo();  
      obj.myMethod();  
   }  
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The final field Demo.MAX_VALUE cannot be assigned

	at beginnersbook.com.Demo.myMethod(Details.java:6)
	at beginnersbook.com.Demo.main(Details.java:10)

Blank final variable

The only variables left are constants. Once a final variable has been initialized, it cannot be changed. The term “blank final variable” refers to a final variable that has not been initialized when it is declared. A compilation error (Error: Variable MAX VALUE may not have been initialized) will be thrown if the blank final variable is not initialized in the class’ constructor.

A class could use a blank last variable as follows:

class Demo{  
   //Blank final variable
   final int MAX_VALUE;
	 
   Demo(){
      //It must be initialized in constructor
      MAX_VALUE=100;
   }
   void myMethod(){  
      System.out.println(MAX_VALUE);
   }  
   public static void main(String args[]){  
      Demo obj=new  Demo();  
      obj.myMethod();  
   }  
}

Output:

100

What good is a final variable that is blank?
Take the Student class, for example, which has a field named Roll No. We may define Roll No as a final variable in a class since it should not be altered after the student has registered, but we cannot initialize Roll No in advance for all the students (otherwise all students would be having same roll no). In this situation, we may designate the roll no variable as final and initialize its value as follows:

class StudentData{  
   //Blank final variable
   final int ROLL_NO;
	 
   StudentData(int rnum){
      //It must be initialized in constructor
      ROLL_NO=rnum;
   }
   void myMethod(){  
      System.out.println("Roll no is:"+ROLL_NO);
   }  
   public static void main(String args[]){  
      StudentData obj=new  StudentData(1234);  
      obj.myMethod();  
   }  
}

Output:

Roll no is:1234

Uninitialized static final variable

Only the static block may initialize a static final variable that was not initialized at declaration. Example:

class Example{  
   //static blank final variable  
   static final int ROLL_NO;
   static{ 
      ROLL_NO=1230;
   }  
   public static void main(String args[]){  
      System.out.println(Example.ROLL_NO);  
   }  
}

Output:

1230

2) final method

An ultimate approach cannot be changed. In other words, a subclass may call the parent class’s final method without any problems, but it cannot override it.

Example:

class XYZ{  
   final void demo(){
      System.out.println("XYZ Class Method");
   }  
}  
	     
class ABC extends XYZ{  
   void demo(){
      System.out.println("ABC Class Method");
   }  
	     
   public static void main(String args[]){  
      ABC obj= new ABC();  
      obj.demo();  
   }  
}

Although the aforementioned program would give a compilation error, we may utilize the final method of the parent class in the subclass without any problems. Look at this code: Since we are not overriding the final method, the program should execute without issue. This demonstrates how final methods are inherited but cannot be overridden.

class XYZ{  
   final void demo(){
      System.out.println("XYZ Class Method");
   }  
}  
	     
class ABC extends XYZ{  
   public static void main(String args[]){  
      ABC obj= new ABC();  
      obj.demo();  
   }  
}

Output:

XYZ Class Method

3) final class

A final class cannot be extended. Think about the following instance:

final class XYZ{  
}  
	     
class ABC extends XYZ{  
   void demo(){
      System.out.println("My Method");
   }  
   public static void main(String args[]){  
      ABC obj= new ABC(); 
      obj.demo();
   }  
}

Output:

The type ABC cannot subclass the final class XYZ

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button