Summary: Final Keyword
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的更多相关文章
- 【笔试题】Java final keyword
Java 知识测试 Java final keyword Question 1 What is the use of final keyword in Java? A. When a class is ...
- java final keyword
依据上下文环境,java的keywordfinal也存在着细微的差别,但通常指的是“这是无法改变的.”不想改变的理由由两种:一种是效率,还有一种是设计.因为两个原因相差非常远,所以关键子final可能 ...
- 又一次认识java(七) ---- final keyword
你总以为你会了,事实上你仅仅是一知半解. final 关键字概览 final关键字可用于声明属性.方法.參数和类,分别表示属性不可变.方法不可覆盖.參数不可变和类不能够继承. 我们来分别看看它的使用方 ...
- Java keyword具体解释
訪问控制修饰符号 1) private 私有的 private keyword是訪问控制修饰符,能够应用于类.方法或字段(在类中声明的变量). 仅仅能在声明 private(内部)类.方 ...
- Java Final and Immutable
1. Final keyword Once a variable X is defined final, you can't change the reference of X to another ...
- 深入理解final关键字以及一些建议
引子:一说到final关键字,相信大家都会立刻想起一些基本的作用,那么我们先稍微用寥寥数行来回顾一下. 一.final关键字的含义 final是Java中的一个保留关键字,它可以标记在成员变量.方法. ...
- 安卓开发(Java)中关于final关键字与线程安全性
前言 学习新知识固然重要,但是时常往回看看,温故知新是很必要的.回顾一下线程安全性和final关键字. 正文 从Java 5开始,final keyword一个特殊用法是在并发库中一个非常重要且经常被 ...
- csharp: Linq keyword example
/// <summary> /// http://www.dotnetperls.com/linq /// </summary> public partial class Li ...
- debug_backtrace final catch
<?php function backtrace_str(){ $str = ''; $w = 0; $backtrace = debug_backtrace(); foreach($backt ...
随机推荐
- OpenMP并行编程
什么是OpenMP?“OpenMP (Open Multi-Processing) is an application programming interface (API) that support ...
- CSS :before和:after (转)
前几天的晚上较全面的去看了下css的一些文档和资料,大部分的样式运用都没什么大问题了,只是有些许较陌生,但是也知道他们的存在和实现的是什么样式.今天主要想在这篇学习笔记中写的也不多,主要是针对:bef ...
- TM2013修改帐号数据目录
M 2013安装以后,聊天记录文件夹默认的保存位置是在“我的文档”中“Tencent Files”,而QQ就可以在软件系统设置中进行指定,但TM2013没有这一栏设置,那么如何才能修改聊天记录文件夹保 ...
- js 判断js函数、变量是否存在
//是否存在指定函数 function isExitsFunction(funcName) { try { if (typeof(eval(funcName)) == "function&q ...
- hive中的常用方法(case,cast,unix_timestamp)
1.case的用法 )格式1 case col when value then '' when value then '' else '' end )格式2 case when col='value' ...
- 在大数据中,关于native包的编译步骤
一.问题的由来: 二.解决问题的方法(所有的操作在root下完成): 1.前期需要的环境,下面的已经在伪分布式中配置好,不再重复 配置好jdk 配置好hadoop 2.上传还需要包 apache-ma ...
- Ubuntu 设置Vim tab为四个空格
使用root权限打开 /etc/vim/vimrc 添加下列配置 set tabstop= set softtabstop= set shiftwidth= set noexpandtab set n ...
- Delphi FindowWindow,FindowWindowEx
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Celery - Best Practices
If you've worked with Django at some point you probably had the need for some background processing ...
- Surround the Trees---hdu1392(凸包GraHam模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:有n棵树,每棵树有一个坐标,想用一些绳子把这些树包含起来,求需要绳子的长度: 就是求凸包的 ...