java final 和C++的const功能类似,用来表达常量,还能用在class或者method上,提供不同的用法。
1. Java Final Variable 
Once a final variable has been assigned, it always contains the same value.但是 static final和private final是有区别的。
这个区别一般体现在类中,在类中,被写为private static final v.s. private final.
(1).什么情况下用private static final,什么情况下用 private final
如果你有一个类,它的instance不管运行多少次,你的意图是它的variable不变化
你那么就该用private static final
如果你有一个类,在它的instance每次运行的生命周期中,你的意图是它的variable不变化
你那么就该用private final
(2).如何用private static final
第一种 正确
class A {private static final int x;static {x = 5;}
}

第二种正确
class A {private static final int x = 5;
...
}
(3). 如何用private final
第一种 正确
class A {private final int x;public A() {x = 5;}
}

第二种 正确
class A {private final int x = 5;...
}

(4) 如果final中存的不是primitive type,是instance of a class, 那么。
If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object(e.g. add an item, delete an item, depending on whether the object is mutable or not), but the variable will always refer to the same object
2. final method
If you make any method as final, you cannot override it.
class Bike{final void run(){System.out.println("running");}
}
class Honda extends Bike{//Output:Compile Time Errorvoid run(){System.out.println("running safely with 100kmph");} public static void main(String args[]){Honda honda= new Honda();honda.run();}
}

3. final class

If you make any class as final, you cannot extend it.
Example of final class
final class Bike{}
//:Compile Time Error
class Honda1 extends Bike{void run(){System.out.println("running safely with 100kmph");}public static void main(String args[]){Honda1 honda= new Honda();honda.run();}
}

4. final parameter
If you declare any parameter as final, you cannot change the value of it.
class Bike11{int cube(final int n){n=n+2;//can't be changed as n is finaln*n*n;
}
public static void main(String args[]){Bike11 b=new Bike11();b.cube(5);}
}

5. 如果要在inner class中使用外层class的变量
When an anonymous inner class is defined within the body of a method, all variables declared final in the scope of that method are accessible from within the inner class. For scalar values, once it has been assigned, the value of the final variable cannot change. For object values, the reference cannot change.
import javax.swing.*;
public class FooGUI {public static void main(String[] args) {//initialize GUI componentsfinal JFrame jf = new JFrame("Hello world!"); //allows jf to be accessed from inner class bodyjf.add(new JButton("Click me"));SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {jf.pack(); //this would be a compile-time error if jf were not finaljf.setLocationRelativeTo(null);jf.setVisible(true);}});}
}

参考资料:
1 http://www.javatpoint.com/final-keyword
2 http://stackoverflow.com/questions/5093744/initialize-a-static-final-field-in-the-constructor
3 http://en.wikipedia.org/wiki/Final_(Java)

JAVA菜鸟入门(8) Java的Final关键字相关推荐

  1. Java 菜鸟入门 | 深入浅出 Java 注解

    注解简介 所谓注解,其实就像一种拥有特定作用的注释,自 JDK1.5 及之后版本所引入的特性,它是放在 Java 源码的类.方法.字段.参数前的一种用作标注的"元数据",与类.接口 ...

  2. C功底挑战Java菜鸟入门概念干货(一)

    一.认识Java 1.Java 程序比较特殊,它必须先经过编译,然后再利用解释的方式来运行.  2.Byte-codes 最大的好处是--可越平台运行,可让"一次编写,处处运行"成 ...

  3. Java面试题,深入理解final关键字

    final关键字 final的简介 final可以修饰变量,方法和类,用于表示所修饰的内容一旦赋值之后就不会再被改变,比如String类就是一个final类型的类. final的具体使用场景 fina ...

  4. 重新精读《Java 编程思想》系列之final关键字

    在java中final关键字标识无法被修改.接下来从final修饰数据.方法和类进行介绍. final数据 final用来告知编译器这一块数据是恒定不变的.数据恒定不变又如下作用: 1.一个永不改变的 ...

  5. Java的this、super和final关键字

    this:(this表示当前对象) 用类名定义一个变量的时候,定义的只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法. 在类的方法定义中使用this关键字代表使用该方法的对象的引用 当必 ...

  6. java本地方法不能是final_Java final关键字

    首页 > 基础教程 > 关键字 > final关键字 Java final关键字 简介 final是java的关键字,它所表示的是"这部分是无法修改的".不想被改 ...

  7. 菜鸟入门:Java程序员学习之路

     1. Java语言基础 谈到Java语言基础学习的书籍,大家肯定会推荐Bruce Eckel的<Thinking in Java>.它是一本写的相当深刻的技术书籍,Java语言基础部分基 ...

  8. java 什么时候用final_java 中 final 关键字的使用

    文章目录 final 保留字概述 final关键字在java中非常重要,它可以应用于类.方法以及变量; final经常和static一起使用来声明常量,你也会看到final是如何改善应用性能的. fi ...

  9. Java面向对象之多态解析、final关键字

    一.多态 1.简单说明 (1)同一个对象拥有多个状态,可以把不同的类型进行统一,让程序具有超强的可扩展性,简化了调用时的操作. (2)"父类=子类"向上赋值:因为子类可以当做父类看 ...

  10. java查询日期类的表,JAVA菜鸟入门篇 - 时间处理相关类实例:打印该月日期表 (29)...

    利用前面我们所学习有关时间处理类,Date.DateFormat.SimpleDateFormat以及Calendar和GregorianCalendar类 编写一个按照用户定义格式(格式:2015- ...

最新文章

  1. ImageView.ScaleType /android:scaleType值的意义区别
  2. 如何从stackoverflow的api 中获取是数据_教你拼多多如何选款、测款,打造出爆款。...
  3. word2vec应用场景_Embedding在腾讯应用宝的推荐实践
  4. 本地window cmd 远程连接外网redis
  5. IPv6套接字编程介绍
  6. Git使用方法——原创
  7. 要是想让程序跳转到绝对地址是0x100000去执行
  8. Teams Bot开发系列:Activity和Turn
  9. 我花了一年时间来学机器学习
  10. 2299元!荣耀X30 12+256GB大内存版开售:窄边直屏天花板
  11. Java贪吃蛇小游戏,我家小AD从小水蛇成长为水中巨蟒!
  12. 决策支持系统(DSS)
  13. “小步快跑、快速迭代” 可用于工作的好方法
  14. python二手房数据分析_使用python抓取分析链家网二手房数据
  15. 千兆万兆以太网测试仪
  16. WEB渗透测试(一)被动信息收集1(DNS信息收集、DNS字典爆破、DNS注册信息)
  17. 2020年新版Java学习路线图最全更新!囊括史上最全面104个知识点
  18. ORA-01558故障恢复----惜分飞
  19. 《代码大全》到底讲什么?
  20. pms酒店管理系统功能,酒店管理系统软件开发

热门文章

  1. filezilla server mysql_教你如何使用filezilla server(教你如何使用filezilla server).doc...
  2. ad怎么批量改元器件封装_在AD软件中的PCB界面如何批量修改封装?
  3. macOS如何刷新DNS缓存
  4. 混淆的艺术-(苍井空变凤姐)Proguard源码分析(三)Proguard配置解析~上
  5. 小程序源码:酒桌扑克娱乐喝酒小游戏微信小程序源码下载多娱乐功能支持流量主
  6. 将小度WiFi改造为无线网卡(小度WiFi能够接收WiFi信号)
  7. MOSFET知识小结
  8. 构建AD域 、 管理AD域
  9. 16、static关键字、接口
  10. C++调用C#创建的COM组件