2016年1月12日 星期二

static vs final on variables & object

//: reusing/FinalData.java                                                                                                                                                                    
// The effect of final on fields.
import java.util.*;
import static net.mindview.util.Print.*;

class Value {
  int i; // Package access
  public Value(int i) { this.i = i; }
}

public class FinalData {
  private static Random rand = new Random(47);
  private String id;
  public FinalData(String id) { this.id = id; }
  // Can be compile-time constants:
  private final int value1 = 9;
  private static int value2 = 19;
  private int value3 = 29;
  private static final int VALUE4 = 99;
  // Typical public constant:
  public static final int VALUE5 = 39;
  // Cannot be compile-time constants:
  private final int i4 = rand.nextInt(20);
  static final int INT_5 = rand.nextInt(20);
  private Value v1 = new Value(11);
  private final Value v2 = new Value(22);
  private static final Value VAL_3 = new Value(33);
  // Arrays:
  private final int[] a = { 1, 2, 3, 4, 5, 6 };
  public String toString() {
    return id + ": " + "i4 = " + i4 + ", INT_5 = " + INT_5 + " value1 = " + value1 + " value2 = " + value2 + " value3 = " + value3 + " VALUE4 = " + VALUE4 + " VALUE5 = " + VALUE5;
  }
  public static void main(String[] args) {
    FinalData fd1 = new FinalData("fd1");
    //fd1.value1++; // Error: can't change value
    fd1.value2++;
    fd1.value3++; //
    fd1.v2.i++; // Object isn't constant!
    fd1.v1 = new Value(9); // OK -- not final
    for(int i = 0; i < fd1.a.length; i++)
      fd1.a[i]++; // Object isn't constant!
    //! fd1.v2 = new Value(0); // Error: Can't
    //! fd1.VAL_3 = new Value(1); // change reference
    //! fd1.a = new int[3];
    print(fd1);
    print("Creating new FinalData");
    FinalData fd2 = new FinalData("fd2");
    print(fd1);
    print(fd2);
  }
} /* Output:
fd1: i4 = 15, INT_5 = 18 value1 = 9 value2 = 20 value3 = 30 VALUE4 = 99 VALUE5 = 39
Creating new FinalData
fd1: i4 = 15, INT_5 = 18 value1 = 9 value2 = 20 value3 = 30 VALUE4 = 99 VALUE5 = 39
fd2: i4 = 13, INT_5 = 18 value1 = 9 value2 = 20 value3 = 29 VALUE4 = 99 VALUE5 = 39
*///:~                                   

沒有留言:

張貼留言