Final Keyword In Java

Introduction

final keyword can be used along with variables, methods and classes.
- variable
- method
- class

Mnemonic

  • A final class cannot be extended by any other class
  • A final variable cannot be reassigned to another value
  • A final method cannot be overridden

Variable

final variable

We cannot change the value of a final variable once it is initialized.

class Student{

    final int studentID = 123;
    public void setID(){
        studentID = 456; //error
    }

    final static int numberOfStuent = 345; 
    static void setNumberOfStudent(){
        numberOfStuent = 789; //error
    }
}

blank final instance variable

  1. A final instance variable that is not initialized at the time of declaration is known as blank final variable.
  2. We must initialize the blank final instance variable in constructor of the class otherwise a compile-time error occurs.. (only initialize in constructor)
class Student{
    final int studentID;
    Student(){
        studentID = 123;
        studentID = 456; //error
    }
}

blank final class variable

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

class Student{
    final static numberOfStuent;
    static{
        numberOfStudent = 789;
        numberOfStuednt = 999; //error
    }
}

final parameter

If you declare any parameter as final, you cannot change the value of it.

class Calculate(
    public static int increase(final int a){
        return ++a; //error
    }
)

Q) Whats the use of blank final variable?

Lets say we have a 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)

class Student{
    final int ROLL_NO;
    Student(int ROLL_NO){
        this.ROLL_NO = ROLL_NO;
    }
}

Method

final method

A final method cannot be overridden.

The Counter is good example:

public class Counter{
    private int counter = 0;
    public final int count(){
        return counter++;
    }
    public final int reset(){
        return (counter = 0);
    }
}

If the public final int count() method is not final,we can do something like this:

Counter c = new Counter(){
    public int count(){
        super.count();
        return super.count();
    }
}
c.count();// not count2

Or something like this:

Counter c = new Counter(){
    public int count(){
        int lastCount = 0;
        for(int i = super.count(); --i >= 0;){
            lastCount = super.count();
        }
        return lastCount;
    }
}
c.count() // Now double count

Q) Is final method inherited?

Yes, final method is inherited but you cannot override.


Class

final class

Final class is complete in nature and can not be subclassed or inherited. Several classes in Java ar e final e.g. String, Integer and other wrapper classes.

Benefits of final keyword in Java

  1. Final keyword improves performance.
  2. Final variable are safe to share in multi-threading environment without additional synchronisation overhead.
  3. Fianl keyword allows JVM to optimised method, variable or class.

Reference

發佈了25 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章