^

Final Method

Methods declared using keyword final are called final methods. Final methods can not be overridden in subclasses. A static method can also be declared final to prevent from overriding.
Syntax:
class className
           {
               final [returnType] methodName()
                {
                    ----------------
                    ----------------
                }
           }
Implementation:
class B{
	final void jobA()	//final method
	    {
		System.out.println(“doing Job A”);
	    }
	void jobB
	   {
		System.out.println(“doing Job B”);
	   }
      }
class D extends B
     {
	    void jobA()		// invalid Overriding
		{
		     System.out.println(“doing Job A differently”);
		}
	    void jobB()		// valid Overriding
		{
		    System.out.println(“doing Job B differently”);
		}
     }

Final Class

A class declared using keyword final is called final class. As a final method can not be overridden, final class can not be inherited.
For Example:
          final class B
              {
                  ------------------
              }
         class D extends B      // Invalid Inheritance
              {           
                 ------------------
              } 
  • It is not necessary for a final class to have final methods.
  • A final class can not have abstract methods.
  • java.lang.String is final class.

Final Variables

A variable declared using keyword final is like constant variable in C/C++. Value assigned once can not be changed later.
A final variable can be initialized either at declaration time or inside constructor functions. No other method can modify its value. If any method try to modify the value, then the compiler shows an error message.
A final variable must be initialized before its use because it does not take any default value like other non-final instance variables.
A static variable can also be declared as final, for example, Thread.MAX_PRIORITY, Thread.MIN_PRIORITY are static and final variables of java.lang.Thread class.

Method parameters can also be declared as final.

Final variables can be of two types:
• final primitive variables.
• final reference variables.

The diff erence between them is that the value of final primitive variable can not be changed once it has been initialized, whereas in case of final reference variable, its reference value can not be changed but the state of the object to which it refers can be changed.
For Example:
class Rect
      {     
      	int l=100,b=20;
      }
class Experiments
	{
	    final int v1=10;	//final primitive variable
	    final int v2;
	    static final int v3=30;    //  static & final primitive variable
	    final Rect ref=new Rect(); //final reference variable
	    public Experiments() 
	           {      v2=20; // initialization of final primitive variable
                  }
	    void showAll()
		{
		      //v1=v1+5;	// Invalid
		      // ref=new Rect();	// Invalid 
		         System.out.println(“v1: “+v1);	 //10
		         System.out.println(“v2: “+v2);	 //20
		         System.out.println(“v3: “+v3);	 //30
		         System.out.println(“ref.l: “+ref.l); // 100
		         ref.l=ref.l+100;	  //valid
		     System.out.println(“After Change Ref.l: “+ref.l); //200
		         final int localFinal=555;	//local final variable
		         System.out.println(“Local Final:”+localFinal);//555
		     //localFinal=666; 	//Invalid
		}
	}
public class FinalVarDemo 
          {    public static void main(String[] args) 
		{
			Experiments ex=new Experiments();
			ex.showAll();
		}
         }
          Output:
                 v1: 10
                 v2: 20
                 v3: 30
                 ref.l: 100
                 After Change Ref.l: 200
                 Local Final:555
          

About the Author
Rajesh K. Bansal (SCJP-Sun Certified Java Programmer)
23 Years experience in Training & Development. Founder of realJavaOnline.com, loves coding in Java(J2SE, J2EE), C++,PHP, Python, AngularJS, Android,MERN Stack(MongoDB,Express,ReactJS,NodeJS). If you like tutorials and want to know more in depth about Java , buy his book "Real Java" available on amazon.in.
#Email : bcebti@gmail.com #Contact : 98722-46056
Available on Amazon
Card image cap
Under the guidance of Founder & Author of "realJavaOnline.com". M:9872246056
Card image cap