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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值