这里介绍一些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基础---->final关键字的使用的更多相关文章

  1. Java基础 -- final关键字

    在java的关键字中,static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.下面我们来了解一下final ...

  2. JAVA 基础--final 关键字的用法

    在java中,final的含义在不同的场景下有细微的差别,in a word,它指的是“不可变的” 1.修饰数据.这里的可以看到被final修饰的变量,值不能被改变,但是 package FinalT ...

  3. JAVA面向对象-----final关键字

    JAVA面向对象-–final关键字 1:定义静态方法求圆的面积 2:定义静态方法求圆的周长 3:发现方法中有重复的代码,就是PI,圆周率. 1:如果需要提高计算精度,就需要修改每个方法中圆周率. 4 ...

  4. 聊聊Java的final关键字

    Java的final关键字在日常工作中经常会用到,比如定义常量的时候.如果是C++程序员出身的话,可能会类比C++语言中的define或者const关键字,但其实它们在语义上差距还是挺大的. 在Jav ...

  5. Java基础-synchronized关键字的用法(转载)

    synchronized--同步 顾名思义是用于同步互斥的作用的. 这里精简的记一下它的使用方法以及意义: 当synchronized修饰 this或者非静态方法或者是一个实例的时候,所同步的锁是加在 ...

  6. Java基础:关键字final,static

    一 . final 含义:adj.最后的,最终的; 决定性的; 不可更改的.在Java中是一个保留的关键字,可以声明成员变量.方法.类以及本地变量.一旦你将引用声明作final,你将不能改变这个引用了 ...

  7. java基础只关键字final

    final关键字简述 final关键字是在编写java程序中出现频率和很高的关键字,如果想要更好的编写java程序,那么掌握final关键字的运用是非常必要的.让我们先看一下final关键字可以修饰的 ...

  8. Java基础super关键字、final关键字、static关键字、匿名对象整理

    super关键字 10.1子父类中构造方法的调用 public class Test { public static void main(String[] args) { new Zi(); } } ...

  9. Java语法基础-final关键字

    final关键字主要用在三个地方:变量.方法.类. 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改: 如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一 ...

随机推荐

  1. 一起talk C栗子吧(第一百三十一回:C语言实例--C程序内存布局三)

    各位看官们,大家好.上一回中咱们说的是C程序内存布局的样例,这一回咱们继续说该样例.闲话休提,言归正转.让我们一起talk C栗子吧. 看官们,关于C程序内存布局的样例,我们在前面的两个章回都介绍过了 ...

  2. ubuntu下载软件安装包

    apt-get -d download xxx ubuntu下载软件安装包命令.仅仅下载deb格式的安装包,不安装. xxx是待下载的安装包.

  3. angular多个控制器如何共享数据

    多个控制器之间共享数据,通常两种方式,一种是在控制器里通过$scope.$$prevSibling或$scope.$$nextSibling获得另一个控制器的作用域对象. 第二种是通过服务的方式,也是 ...

  4. 孙源即将分享 DynamicCocoa 实现细节

    孙源即将分享 DynamicCocoa 实现细节   我的公众号之前发的一文中提到滴滴做了一个很牛逼的动态化方案 DynamicCocoa.该方案设计得非常精巧,解决了两种不同的语言在代码上如何等价生 ...

  5. SAP ERP 6.0 EHP7 SR2(WINDOWS MSSQL版)安装说明

    原文 by 枫竹丹青 ⋅ 1.安装准备 1.1.版本说明 本文是描述在一个Windows虚拟机.SQL Server数据库环境下,安装SAP ERP 6.0 EHP7 SR2服务器,安装完成虚拟机文件 ...

  6. Jquery 事件执行两次

    js(jquery)的on绑定点击事件执行两次的解决办法—不是事件绑定而是事件冒泡 阻止冒泡的方法并不止 return false 这一种,还有event.stopPropagation(),这两种方 ...

  7. mysql数据库批量操作

    批量KILL会话: 1.首先,根据条件将查询到要kill的进程写入文件:如:desc information_schema.processlist; SELECT concat('KILL ',id, ...

  8. spark读取本地文件

    /** * Read a text file from HDFS, a local file system (available on all nodes), or any * Hadoop-supp ...

  9. 转:linux添加用户

    功能说明:建立用户帐号. 语 法:useradd [-mMnr][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][- ...

  10. kernel 4.4.12 移植 HUAWEI MU609 Mini PCIe Module

    首先请参考 http://www.cnblogs.com/chenfulin5/p/6951290.html 上一章刚讲了 kernel 3.2.0 移植 MU609 这一章记录新版kernel 的移 ...