Exercise 18

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41282486/article/details/84112951
//: reusing/E18_FinalFields.java
/****************** Exercise 18 *****************
 * Create a class with a static final field and a
 * final field and demonstrate the difference
 * between the two.
 ***********************************************/
package reusing;
class SelfCounter {
 private static int count; 
 Thinking in Java, 4th 94 Edition Annotated Solution Guide
 private int id = count++;
 public String toString() { return "SelfCounter " + id; }
}
class WithFinalFields {
 final SelfCounter scf = new SelfCounter();
 static final SelfCounter scsf = new SelfCounter();
 public String toString() {
 return "scf = " + scf + "\nscsf = " + scsf;
 }
}
public class E18_FinalFields {
 public static void main(String args[]) {
 System.out.println("First object:");
 System.out.println(new WithFinalFields());
 System.out.println("Second object:");
 System.out.println(new WithFinalFields());
 }
} /* Output:
First object:
scf = SelfCounter 1
scsf = SelfCounter 0
Second object:
scf = SelfCounter 2
scsf = SelfCounter 0
*///:~
Because class loading initializes the static final, it has the same value in both
instances of WithFinalFields, whereas the regular final’s values are different
for each instance.

猜你喜欢

转载自blog.csdn.net/weixin_41282486/article/details/84112951