博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Summary: Final Keyword
阅读量:6671 次
发布时间:2019-06-25

本文共 1220 字,大约阅读时间需要 4 分钟。

In this tutorial we will learn the usage of final keyword. final keyword can be used along with variables, methods and classes. We will cover following topics in detail.

1) final variable

2) final method
3) final class

 

1. Final Variables

final variables are nothing but constants. We cannot change the value of a final variable once it is initialized. Lets have a look at the below code:

class Demo{
final int MAX_VALUE=99; void myMethod(){ MAX_VALUE=101; } public static void main(String args[]){ Demo obj=new Demo(); obj.myMethod(); } }

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The final field Demo.MAX_VALUE cannot be assigned at beginnersbook.com.Demo.myMethod(Details.java:6) at beginnersbook.com.Demo.main(Details.java:10)

2. Final Methods

A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it.

final modifier indicates that the method may not be overridden in a derived class.

3. Final Class

We cannot extend a final class. 

When applied to a class, the final modifier indicates that the class can not be used as a base class to derive any class from it. 

转载地址:http://gulxo.baihongyu.com/

你可能感兴趣的文章
get改造成post请求
查看>>
linux文件分割(将大的日志文件分割成小的)
查看>>
使用LNMP常见问题解答
查看>>
python网络爬虫进入(一)——简单的博客爬行动物
查看>>
好玩的SQL
查看>>
TFS Express backup and restore
查看>>
fastjson初始化对性能的影响(转)
查看>>
确定只出现曾有两位数字数组
查看>>
Facebook 调试工具Stetho配置入门
查看>>
win7安装ruby on rails开发环境
查看>>
.Net程序员安卓学习之路6:等待条
查看>>
[CareerCup] 6.3 Water Jug 水罐问题
查看>>
jQ效果:地址栏链接与a标签链接匹配
查看>>
poj 3180 The Cow Prom(tarjan+缩点 easy)
查看>>
【开源】Ionic项目实例《Ionic中文社区》
查看>>
求两个对角向上、列索引是偶数的元件和。
查看>>
ASP.NET农历时间显示(两)
查看>>
Java,JSP,JavaScript三和差异
查看>>
MySQL 调优基础(二) Linux内存管理
查看>>
netperf 而网络性能测量
查看>>