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变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改: 如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一 ...
随机推荐
- [翻译] C# 8.0 新特性 Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南) 【由浅至深】redis 实现发布订阅的几种方式 .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐
[翻译] C# 8.0 新特性 2018-11-13 17:04 by Rwing, 1179 阅读, 24 评论, 收藏, 编辑 原文: Building C# 8.0[译注:原文主标题如此,但内容 ...
- 摘:常用函数(包括:宽字符函数、普通C函数 )
只要看见“W”就是宽的意思,左边wchar_t,右边char 字符分类: 宽字符函数 普通C函数 描述 iswalnum() isa ...
- unity, 不要change Default sharedMaterial
假设在场景中加一个sprite,其材质使用默认的Sprites-Default. 若调用: Color color=sprite.GetComponent<SpriteRenderer>( ...
- Atitit.软件仪表盘(8)--os子系统--资源占用监测
Atitit.软件仪表盘(8)--os子系统--资源占用监测 CPU使用 内存使用 磁盘队列 任务管理器 网络速度 插件列表( 资源管理器插件,浏览器插件,360optim) 启动项管理 (350) ...
- atitit。gui 界面皮肤以及换肤总结 java .net c++
atitit.gui 界面皮肤以及换肤总结 java .net c++ 1. Swing 的皮肤 1 1.1. windows风格 1 1.2. Mac风格 ( liquid 框架) 1 2. 如何给 ...
- BS Web窗体 动态修改WebConfig文件参数及数据库链接串
WebConfig操作帮助类 /// /// ConfigurationOperator 的摘要说明 /// public class ConfigurationOperator : IDisposa ...
- UIWindow的一点儿思考
转自:http://www.cnblogs.com/smileEvday/archive/2012/11/16/UIWindow.html 每一个IOS程序都有一个UIWindow,在我们通过模板简历 ...
- nginx rewrite目录对换
/123/xxx----->xxx?id=123 [root@web01 default]# pwd /app/www/default [root@web01 └── sss └── index ...
- 让低版本IE也能正常运行HTML5+CSS3网站的3种解决方案
现在我们可以选择浏览器非常多,所以浏览器的环境也是种类繁多,同一个浏览器也是包含各种不同的版本,不同的版本之间的渲染方法也存在差异,,它们支持的 HTML5.CSS3 特性恐怕也不尽相同.这种情况于是 ...
- Tomcat7中开启gzip压缩功能的配置方法
使用gzip压缩可以减少数据传输大小,加快网页加载速度.很多大站都开启了gzip压缩,不过也有很多网站并没有开启gzip压缩,上次看了一篇文章说开启gzip压缩后对搜索引擎不友好,但从带宽和流量的角度 ...