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变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改: 如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一 ...
随机推荐
- TocControl控件图层无法显示问题
在窗口里的层层嵌套SplitContainer后,出现最内层SplitContainer内部TocControl控件图层无法显示问题:加载完mxd后代后加上axTOCControl1.SetBuddy ...
- WinForm下的键盘事件(KeyPress、KeyDown)及如何处理不响应键盘事件
KeyDown事件用来处理功能键:F1 F2 F3... keyPress事件用来处理字符比如说:A B C... 1 2 3... 注:处理该事件时,需要先将窗体的 KeyPreview=true; ...
- jQuery knowledge
I have used jquery for many years, but didn't list the problem I ever meeting, so here is a list of ...
- Xilinx问题查找
1.登录https://www.xilinx.com/ 2.在All下拉菜单选择Support选项查找技术问题,All选项会查找全部关键词,但是大多数我们只需要技术相关的内容
- SQL业务审核与优化
审核 什么是业务审核 类似与code review 评审业务Schema和SQL设计 偏重关注性能 是业务优化的主要入口之一 审核提前发现问题,进行优化 上 ...
- php7性能、兼容性和稳定性探讨
前几天看到php7发布了beta1版本,想了解一下php7到底折腾了些啥东西出来.这一了解发现不得了了,改变还挺多的.最最重要的方面就是性能提升了不少,这边有一个pdf文件是惠新宸(鸟哥,php核心开 ...
- Hystrix的原理与使用
转载自:https://segmentfault.com/a/1190000005988895 http://blog.csdn.net/xiaoyu411502/artic ...
- line: 1: Syntax error: word unexpected (expecting ")")
开发板上运行可执行程序报出错误: line1: 1: Syntax error: word unexpected (expecting ")") 解决思路: 1.编译器的问题 用a ...
- ubuntu时钟不显示的解决方法
原文链接:http://muzi.info/articles/529.html 有时候我们会看到我们电脑的状态栏那里并没有显示时间,一个原因是日期时间指示器没有工作,另一个可能的原因是用户禁用了时间显 ...
- JSF request参数传递
转载自:http://blog.csdn.net/duankaige/article/details/6711044 1:JSF页面之间传参 方法1: <h:outputLink value=& ...