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.

Summary: Final Keyword的更多相关文章

  1. 【笔试题】Java final keyword

    Java 知识测试 Java final keyword Question 1 What is the use of final keyword in Java? A. When a class is ...

  2. java final keyword

    依据上下文环境,java的keywordfinal也存在着细微的差别,但通常指的是“这是无法改变的.”不想改变的理由由两种:一种是效率,还有一种是设计.因为两个原因相差非常远,所以关键子final可能 ...

  3. 又一次认识java(七) ---- final keyword

    你总以为你会了,事实上你仅仅是一知半解. final 关键字概览 final关键字可用于声明属性.方法.參数和类,分别表示属性不可变.方法不可覆盖.參数不可变和类不能够继承. 我们来分别看看它的使用方 ...

  4. Java keyword具体解释

    訪问控制修饰符号 1)        private 私有的 private keyword是訪问控制修饰符,能够应用于类.方法或字段(在类中声明的变量). 仅仅能在声明 private(内部)类.方 ...

  5. Java Final and Immutable

    1. Final keyword Once a variable X is defined final, you can't change the reference of X to another ...

  6. 深入理解final关键字以及一些建议

    引子:一说到final关键字,相信大家都会立刻想起一些基本的作用,那么我们先稍微用寥寥数行来回顾一下. 一.final关键字的含义 final是Java中的一个保留关键字,它可以标记在成员变量.方法. ...

  7. 安卓开发(Java)中关于final关键字与线程安全性

    前言 学习新知识固然重要,但是时常往回看看,温故知新是很必要的.回顾一下线程安全性和final关键字. 正文 从Java 5开始,final keyword一个特殊用法是在并发库中一个非常重要且经常被 ...

  8. csharp: Linq keyword example

    /// <summary> /// http://www.dotnetperls.com/linq /// </summary> public partial class Li ...

  9. debug_backtrace final catch

    <?php function backtrace_str(){ $str = ''; $w = 0; $backtrace = debug_backtrace(); foreach($backt ...

随机推荐

  1. Smarty 配置文件的读取

    http://www.cnblogs.com/gbyukg/archive/2012/06/12/2539067.html

  2. CC2540 USB DONGLE 使用 BTool 调试BLE 说明

    一.Btool软件界面介绍 首先您要将USBDONGLE插入电脑的USB口,然后打开双击打开Btool软件,打开后如下图所示: 在安装驱动的教程中,我们已经记住了我们的USB DONGLE的串口号,在 ...

  3. Linux环境下配置eclipse,以及创建maven工程

    一:maven的安装 1.安装配置maven环境变量 2.验证 二:eclipse的安装 3.解压配置eclipse 4.启动eclipse,必须在虚拟机的eclipse下启动 5.结果 三:修改配置 ...

  4. php--纯静态和伪静态的区别与关系

    先前说了什么是纯静态和伪静态,现在介绍一下他们的区别? 首先肯定的是纯静态和伪静态都是SEO的产物,但纯静态和伪静态还是有很大区别的.纯静态是生成真实的HTML页面保存到服务器端,用户访问时直接访问这 ...

  5. JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的 开源 Java EE快速开发平台

    JeeSite本身是以Spring Framework为核心容器,Spring MVC为模型视图控制器,MyBatis为数据访问层, Apache Shiro为权限授权层,Ehcahe对常用数据进行缓 ...

  6. magento安装新插件后后台配置空白解决办法

    前段时间,安装完Magento插件以后,就会出现空白或者404问题,在某些运营中的magento网站,安装新插件后后台配置空白解决. 1 将sysytem->toos->Compilati ...

  7. Using Change Management and Change Control Within a Project

    In any project, change is inevitable whether it comes from within the project or from external sourc ...

  8. @Mybatis传多个参数

    首选还是按照面向对象的方式执行sql.但是有时候入参对象嵌套的比较深,类中有类,面向对象就不太好处理了 主要有以下两种方式 1.DAO层的函数方法 public User selectUser(Str ...

  9. iOS 各尺寸iPhone分辨率

  10. 深度实践KVM笔记

    深度实践KVM笔记 libvirt(virt-install,API,服务,virsh)->qemu(qemu-kvm进程,qemu-img)->KVM虚拟机->kvm.ko 内核模 ...