简体   繁体   English

Java中的最终和静态最终用法

[英]Final and Static Final usage in Java

This is from the Ex18 of Chapter "Resuing Classes" in "Thinking in Java". 这来自“ Java思维”中“恢复类”一章的Ex18。 The code is as below: 代码如下:

 public class E18 {
  public static void main(String[]args)
   {
    System.out.println("First object:");
    System.out.println(new WithFinalFields());
    System.out.println("First object:");
    System.out.println(new WithFinalFields());
   }

  }

class SelfCounter{
 private static int count;
 private int id=count++;
 public String toString()
 {
    return "self counter"+id;
 }

 }

class WithFinalFields
{
  final SelfCounter selfCounter=new SelfCounter();
  static final SelfCounter scsf=new SelfCounter();

      public String toString()
      {
           return "selfCounter="+selfCounter+"\nscsf="+scsf;
      }

}

The output of the code is: 代码的输出为:

 First object:
 selfCounter=self counter1
 scsf=self counter0

 First object:
 selfCounter=self counter2
 scsf=self counter0

I could understand why in both runs the scsf instance always gets the id assigned to 0 since it is declared to be a final and static field. 我能理解为什么在这两次运行中,由于声明为final和static字段,因此scsf实例始终将ID分配为0。 What confuses me is why the id of "selfCounter" objects gets assigned to be 1 and 2 respectively, I am a bit stuck on how the calculation of id is carried out based on another static instance variable--"count". 令我感到困惑的是为什么将“ selfCounter”对象的id分别指定为1和2,我对如何基于另一个静态实例变量“ count”进行id的计算有些困惑。

Thanks for the guidance. 感谢您的指导。

The ids are 1 and 2 because you're making three SelfCounter objects, all of which share the same count field, which is initialized implicitly to zero. ID为1和2,因为您要创建三个 SelfCounter对象,所有这些对象共享相同的计数字段,该字段隐式初始化为零。

The first one is the static SelfCounter in WithFinalFields. 第一个是WithFinalFields中的静态SelfCounter。 Its ID is zero because the count field is implicitly initialized to zero, and the value of count++ is zero. 它的ID为零,因为count字段被隐式初始化为零,而count ++的值为零。

The second ID is one, because the value of count++ is one. 第二个ID为1,因为count ++的值为1。 And the third ID is then two, because the value of count++ is two. 然后,第三个ID为2,因为count ++的值为2。

The variable private static int count; 变量private static int count; is a static variable, it will have a value of 0 when the program is launched and will not be recreated. 是静态变量,启动程序时它将具有0值,并且不会重新创建。 private int id=count++; is a dynamic variable. 是动态变量。
You have 3 cases when a new instance of the SelfCounter() class is created: 1 because of the line static final SelfCounter scsf=new SelfCounter(); 在创建SelfCounter()类的新实例时,有3种情况:1,因为该行的static final SelfCounter scsf=new SelfCounter(); , 1 when the new WithFinalFields() is launched for the first time, and 1 when it is launched for the second time. ,第一次启动new WithFinalFields()时返回1,第二次启动new WithFinalFields()时返回1。 So, the values during the first and the second launch of WithFinalFields() will be 1 and 2 respectively. 因此, WithFinalFields()的第一次和第二次启动期间的值将分别为1和2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM