java基础---->final关键字的使用
这里介绍一些java基础关于final的使用,文字说明部分摘自java语言规范。心甘情愿这四个字,透着一股卑微,但也有藏不住的勇敢。
Final关键字的说明
一、关于final变量规范说明
、A final variable may only be assigned to once.
、Once a final variable has been assigned, it always contains the same value.
、If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
、A variable of primitive type or type String , that is final and initialized with a compile-time constant expression, is called a constant variable.
二、关于final类的规范说明
、A class can be declared final if its definition is complete and no subclasses are desired or required.
、It is a compile-time error if the name of a final class appears in the extends clause of another class declaration; this implies that a final class cannot have any subclasses.
、It is a compile-time error if a class is declared both final and abstract , because the implementation of such a class could never be completed.
、Because a final class never has any subclasses, the methods of a final class are never overridden.
三、关于final学习的测试代码如下
- final变量的代码测试:
/**
* Created by huhx on 2017-05-12.
*/
public class FinalFiledTest {
final String string = "hello world";
final Map<String, String> map = new HashMap<>(); public static void main(String[] args) {
FinalFiledTest finalTest = new FinalFiledTest();
// finalTest.string = "world hello"; // cannot assign a value to final variable "string"
finalTest.map.put("username", "linux");
Map<String, String> map2 = new HashMap<>();
// finalTest.map = map2; // cannot assign a value to final variable "map"
System.out.println(finalTest.map.get("username"));
}
}
- final方法的代码测试:
/**
* Created by huhx on 2017-05-12.
*/
public class FinalMethodTest {
public static void main(String[] args) {
Women women = new Women();
// women.username; // 没有权限使用
women.sayWorld();
// women.sayHuhx(); // 没有权限调用
}
} class People {
private String username;
String address; People() {
this.username = "huhx";
this.address = "china";
} public void sayHello() {
System.out.println("Hello");
} public final void sayWorld() {
System.out.println("World");
} private void sayHuhx() {
System.out.println("Huhx");
}
} class Women extends People {
// 不能重写父类的final方法,但是可以在子类中使用
// public void sayWorld() {
// System.out.println("World");
// }
}
- final类的代码测试:
/**
* Created by huhx on 2017-05-12.
*/
// cnanot inherit from final "com.linux.huhx.finalTest.Person"
//public class FinalClassTest extends Person{
//
//} final class Person {
private String username; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
}
} // modifier "final" not allowed here, 接口不能用final修饰
//final interface Human {
//
//}
友情链接
- java的语言规范pdf下载: http://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf
java基础---->final关键字的使用的更多相关文章
- Java基础 -- final关键字
在java的关键字中,static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.下面我们来了解一下final ...
- JAVA 基础--final 关键字的用法
在java中,final的含义在不同的场景下有细微的差别,in a word,它指的是“不可变的” 1.修饰数据.这里的可以看到被final修饰的变量,值不能被改变,但是 package FinalT ...
- JAVA面向对象-----final关键字
JAVA面向对象-–final关键字 1:定义静态方法求圆的面积 2:定义静态方法求圆的周长 3:发现方法中有重复的代码,就是PI,圆周率. 1:如果需要提高计算精度,就需要修改每个方法中圆周率. 4 ...
- 聊聊Java的final关键字
Java的final关键字在日常工作中经常会用到,比如定义常量的时候.如果是C++程序员出身的话,可能会类比C++语言中的define或者const关键字,但其实它们在语义上差距还是挺大的. 在Jav ...
- Java基础-synchronized关键字的用法(转载)
synchronized--同步 顾名思义是用于同步互斥的作用的. 这里精简的记一下它的使用方法以及意义: 当synchronized修饰 this或者非静态方法或者是一个实例的时候,所同步的锁是加在 ...
- Java基础:关键字final,static
一 . final 含义:adj.最后的,最终的; 决定性的; 不可更改的.在Java中是一个保留的关键字,可以声明成员变量.方法.类以及本地变量.一旦你将引用声明作final,你将不能改变这个引用了 ...
- java基础只关键字final
final关键字简述 final关键字是在编写java程序中出现频率和很高的关键字,如果想要更好的编写java程序,那么掌握final关键字的运用是非常必要的.让我们先看一下final关键字可以修饰的 ...
- Java基础super关键字、final关键字、static关键字、匿名对象整理
super关键字 10.1子父类中构造方法的调用 public class Test { public static void main(String[] args) { new Zi(); } } ...
- Java语法基础-final关键字
final关键字主要用在三个地方:变量.方法.类. 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改: 如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一 ...
随机推荐
- HDU 3746 数据结构之KMP
pid=3746">点击打开链接 题意:给T组数据,每组一个字符串,问最少加入多少个字符能够使这个串变成一个子串连续出现的串 思路:利用KMP的next数组进行变换,next数组保存的 ...
- MFC显示GIF动画图片
本帖则将讨论如何在MFC的对话框里显示GIF动画图片.一些关于传统控件的美化方法正在研究当中会陆续发帖的. 这是本帖用到的一个VS2008例程. 附件 GifPicture.rar (138.1 ...
- Aperture Time与NPLC
NPLC工频周期数 NPLC是采样电源的周期倍数,N代表是多少倍,PLC与采样电源有关 交流电源的干扰是很厉害的.为了减少交流电源的干扰,一个常用的方法就是把测量周期尽可能的取成交流周波的整数倍,这样 ...
- ToString(“N2”)和ToString(“0.00”)之间的区别
看来N会包含数千个分隔符,而0.00则不会. N2将以500.00的方式工作,但是当您有5000.00时,N2将显示为 5,000.00 代替 5000.00 If you do this inste ...
- Experience on Namenode backup and restore --- checkpoint
Hadoop version: Hadoop 2.2.0.2.0.6.0-0009 Well, We can do this by building Secondary Namenode, Check ...
- docker使用问题总结
1. docker报[Error response from daemon: Error running DeviceCreate (createSnapDevice) dm_task_run fai ...
- jfinal中Interceptor的使用
一.拦截器是用于对action请求的拦截处理,发生在进入action方法体之前的拦截操作,这样方便了对请求实例做一些文章. 二.自定义.系统已有拦截器都需要实现Interceptor接口,这样才能 ...
- 【安卓】自己定义基于onDraw的随意动画(不不过平移/旋转/缩放/alpha)、!
思路: 1.基于时间的显示映射.如:给定度数,显示圆弧,加上时序,就可以有圆弧动画的效果 2.给定时序. 用于驱动动画的一帧帧绘制 方案一.基于ObjectAnimator.动画运作时会调用degre ...
- Elk使用笔记(坑)(2017-02-17更新)
Elk使用笔记(坑)(2017-02-17更新) 作者: admin 时间: 2016-12-07 分类: 工具,数据 主要记录使用过程终于到的一些坑和需要注意的地方,有些坑想不起来了,以后再完善补上 ...
- 在 Java 8 中获取日期
前言 前面一篇文章写了<SimpleDateFormat 如何安全的使用?>, 里面介绍了 SimpleDateFormat 如何处理日期/时间,以及如何保证线程安全,及其介绍了在 Jav ...