Final In Java

Final In Java :

  • final is keyword. final is modifier.
  • If we can’t to change the value of the variable then those variable value must be made a constant. Constant is identifier whose value cannot be changed during execution of the program. Java make anything constant we can declare final keyword.
  • final declare constants. There can be final variables, methods and classes.

final can be applied

  1. final keyword
  2. final method
  3. final variable
Final In Java

final keyword :

  • Variable can be declared using the keyword final. Then final variable can be assigned only once.
  • It is read only. Final variable cannot be modified by any statement in the program.
  • final must be initialized a final variable when it is declared.

For example:

finalint FILENEW = 1;

  • final keyword can be used many contexts. final can be variable, method and class.
  • The final keyword can be applied with variable 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 with will be initialized in the static block only.

Difference :

(static inti = 10 and final static inti = 10) both can’t be change value. Static variable given the value not at that time but final variable value given to final variable must be given at that time of declaration.

Advantage :

  • final keyword is we can achieve security as no one is allowed to change our implementation.

Disadvantage :

  • Missing by the OOP’s inheritance and polymorphism (overriding) then if there is no specific requirement never recommended to use final keyword.

Question

  1. Abstract Class vs Final Class
  • Inheritance can be allowed. – Inheritance cannot be allowed.
  • It contains abstract method. – It cannot contain abstract method.
  • Abstract method must be overridden. – Final method cannot be overridden.

final variable :

  • final variable in java cannot be change the value of variable.
class FinalVariable {
    final intspeedlimit = 90;//final variable  

    void run() {
speedlimit = 400;
    }

    public static void main(String args[]) {
FinalVariableobj = new FinalVariable();
obj.run();
    }
}
  • Student class which is having a field called Roll No. Since Roll No should not be changed once the student is registered, we can declare it as a final variable in a class but we cannot initialize roll no in advance for all the students(otherwise all students would be having same roll no). In such case we can declare roll no variable as blank final and we initialize this value during object creation
class StudentData{  
   //Blank final variable
   final int ROLL_NO;
	
StudentData(intrnum){
      //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[]){  
StudentDataobj=new  StudentData(1234);  
obj.myMethod();  
   }  
}

final method :

  • final method, you cannot override it.final method is inherited.
class Bike {
    final void run() {
System.out.println("running");
    }
}

class FinalMethod extends Bike {
    @Override
    void run() {
System.out.println("running safely with 100kmph");
    }

    public static void main(String args[]) {
FinalMethodhonda = new FinalMethod();
honda.run();
    }
}
class Bike {

    final void run() {
System.out.println("running...");
    }
}

class Honda2 extends Bike {
    public static void main(String args[]) {
        new Honda2().run();
    }
}

final class :

  • If you make any class as final, you cannot extend it.final classes available in Java one of them is Java String class.

For example :

  • All wrapper class in java like, float, integer etc. are Java final Class and we cannot extend them.
final class Bike {
}

class FinalClass extends Bike {
    void run() {
System.out.println("running safely with 100kmph");
    }

    public static void main(String args[]) {
FinalClasshonda = new FinalClass();
honda.run();
    }
}

blank or uninitialized final variable :

  • A final variable that is not initialized at the time of declaration is known as blank final variable.
class Student{  
int id;  
String name;  
final String PAN_CARD_NUMBER;  
...  
} 

Can we initialize blank final variable?

  • Yes, but only in constructor.
class Bike{
    final intspeedlimit;//blank final variable  

    Bike() {
speedlimit = 70;
System.out.println(speedlimit);
    }

    public static void main(String args[]) {
        new Bike();
    }
}

static blank final variable :

  • static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.
class A {
    static final int data;//static blank final variable  

    static {
        data = 50;
    }

    public static void main(String args[]) {
System.out.println(A.data);
    }
}

What is final parameter?

  • If you declare any parameter as final, you cannot change the value of it.
class Bike {
int cube(final int n) {
int n = n + 2;//can't be changed as n is final  
        return n * n * n;
    }

    public static void main(String args[]) {
        Bike b = new Bike();
b.cube(5);
    }
}

Can we declare a constructor final?

  • No, because constructor is never inherited.

Final Fields :

  • Final fields can be either constants or write-once fields.

Reference Final Variable :

  • When a Java final variable may refer to an object, then the final variable known is as reference final variable.
public class FinalReferenceVariable {

    public static void main(String[] args) {
        final StringBuilderstringbufferVariable = new StringBuilder("Data");
System.out.println(stringbufferVariable);
stringbufferVariable.append("Flair");
System.out.println(stringbufferVariable);
    }
}

Points to Remember

  • A constructor cannot be declared as final.
  • Local final variable must be initializing during declaration.
  • All variables declared in an interface are by default final.
  • We cannot change the value of a final variable.
  • A final method cannot be overridden.
  • A final class not be inherited.
  • If method parameters are declared final then the value of these parameters cannot be changed.
  • It is a good practice to name final variable in all CAPS.
  • final, finally and finalize are three different terms. finally is used in exception handling and finalize is a method that is called by JVM during garbage collection.
  • final keyword in a class declaration doesn’t mean that the objects of this class are immutable.
  • final variables are safely shared in multithreading environment without additional synchronization overhead.
  • final keyword allows JVM to optimized method, variable or class.