• Java:The final Keyword


     看不懂

     1 import java.util.*;
     2 
     3 class Value {
     4     int i;
     5 
     6     public Value(int i) {
     7         this.i = i;
     8     }
     9 }
    10 
    11 public class FinalData {
    12 
    13     private static Random rand = new Random(47);
    14     private String id;
    15 
    16     public FinalData(String id) {
    17         this.id = id;
    18     }
    19     //can be compile-time constants:
    20     private final int valueOne = 9;
    21     private static final int VALUE_TWO = 99;
    22     //Typical public constant:
    23     public static final int VALUE_THREE = 39;
    24     //cannot be compile-time constants
    25     private final int i4 = rand.nextInt(20);
    26     static final int INT_5 = rand.nextInt(20);
    27     private Value v1 = new Value(11);
    28     private final Value v2 = new Value(22);
    29     private static final Value VAL_3 = new Value(33);
    30     private final int[] a = { 1, 2, 3, 4, 5, 6 };
    31 
    32     public String toString() {
    33         return id + ": i4 = " + i4 + ", INT_5 = " + INT_5;
    34     }
    35 
    36     public static void main(String[] args) {
    37         FinalData fd1 = new FinalData("fd1");
    38         //!fd1.valueOne++; //Error:cannot change value
    39         fd1.v2.i++;//Object is not constant
    40         fd1.v1 = new Value(9);//OK--not final
    41         for (int i = 0; i < fd1.a.length; i++) {
    42             fd1.a[i]++; //Object is not constant
    43         }
    44         //!fd1.v2=new Value(0); //Error:cannot
    45         //!fd1.VAL_3=new Value(1);//change reference
    46         //!fd1.a=new int[3];
    47         System.out.println(fd1);
    48         System.out.println("creating new FinalData");
    49         FinalData fd2 = new FinalData("fd2");
    50         System.out.println(fd1);
    51         System.out.println(fd2);
    52     }
    53 }

    输出

    fd1: i4 = 15, INT_5 = 18
    creating new FinalData
    fd1: i4 = 15, INT_5 = 18
    fd2: i4 = 13, INT_5 = 18

     Since valueOne and VALUE_TWO are final primitives with compile-time values , they can both the used as compile-time constants and are not different in any importent way. VALUE_THREE is the more typical way you'll see such constants defined:public so they're usable outside the package, static to emphasize that there's only one, and final to say that it'sa constant. Note that final static primitives with constant initial values(that is,compile-time constants)are named with all capitals by convention, with words separeted by underscores.(This is just like C constants, which is where the convention originated.)

    Just because something is final doesn't mean that its value is known at compile time, This is demonstrated by initializing i4 and INT_5 at run time using randomly generated numbers. This portion of the example also shows the difference between making a final value static or non-static. This difference shows up only when the values are initialized at run time ,since the compile-time values are treated the same by the compiler.(And presumably optimized out of existence.)The difference is shown when you run the program. Note that the values of i4 for fd1 and fd2 are unique. but the value for INT_5 is not changed by creating the second FinalData object. That's because it's static and is initialized once loading and not each time a new object is created.

    The variables v1 throught VAL_3 demonstrated the meaning of a final reference. As you can see in main(),just because v2 is final doesn't mean that you can't change its value. Because it's  a reference, final means that you cannot rebind v2 to a  new object.You can also see that the same meaning holds true for an array, which is just another kind of refeence.(There is no way that I know of to make  the array references themselves final.) Making references final seems less useful than making primitives final.

  • 相关阅读:
    如何在android studio上加入OpenCV库
    c++ overload 、override、overwrite
    学习笔记:linux之文件空洞
    windows 编译安装PROJ.4
    RDD:基于内存的集群计算容错抽象
    用Scala语言轻松开发多线程、分布式以及集群式程序
    scala 读雷达数据文件,生成png
    linux下virtualBox挂载物理磁盘,启动第二块硬盘中的系统
    shell检查网络出现异常、僵尸进程、内存过低后,自动重启
    cordova 5.0版本说明
  • 原文地址:https://www.cnblogs.com/taoxiuxia/p/4451751.html
Copyright © 2020-2023  润新知