final_Keyword

final Java Keyword with Examples

As we know that inheritance enables us to reuse existing code, sometimes we do need to set limitations on extensibility for various reasons; the final keyword allows us to do exactly that.

In this short article, we’ll take a look at what the final keyword means for classes, methods, and variables -

  • The final keyword may be applied to a variable, indicating the value of the final variable can not be changed (It will be constant).
  • The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
  • The final keyword may be applied to a method, indicating the method may not be overridden in any subclass
  • A final variable that has no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.

Let’s discuss usage of a final keyword with respect to variable, method, and class.

Java final variable

If you make any variable as final, you cannot change the value of a final variable(It will be constant).
Example of final variable
The below diagram demonstrates that the final variable name can’t be reassigned which will result in compile time error.

Java final method

The final keyword may be applied to a method, indicating the method may not be overridden in any subclass.

When we design a class and feel that a method shouldn’t be overridden, we can make this method final. We can also find many final methods in Java core libraries.

Sometimes we don’t need to prohibit a class extension entirely, but only prevent overriding of some methods. A good example of this is the Thread class. It’s legal to extend it and thus create a custom thread class. But its isAlive() methods is final.

Java final method example

The below diagram demonstrates that the final method can’t be overridden which will result in compile time error:

Java final class
The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
If we look at the code of Java core libraries, we’ll find many final classes there. For example String, Wrapper classes and BigDecimal, BigInteger etc.
Java final class example
The below diagram demonstrates that the final class can’t be extended which will result in compile time error.

What is blank or uninitialized final variable?
A final variable that is not initialized at the time of declaration is known an as a blank final variable.
If you want to create a variable that is initialized at the time of creating an object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.
It can be initialized only in the constructor.

Example of blank final variable

class Student{  
    int id;  
    String name;  
    final String PAN_CARD_NUMBER;  
...  
}  

Note that the final PAN_CARD_NUMBER variable not initialized. We can initialize a blank final variable only in a constructor.

Summary

  • Variables marked as final can’t be reassigned.
  • Methods marked as final cannot be overridden.
  • Classes marked as final can’t be extended.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值