8. วัตถุสิ่งของ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 8-41  คลาส Value ควบคุมการผลิตอ็อบเจกต์เพียง 4 อ็อบเจกต์เท่านั้น
public class Value {
  private static Value[] value = {new Value(0), new Value(1),  
                                  new Value(2), new Value(3)};
  private final int v;
  private Value(int v) { this.v = v; }

  public static Value getInstance(int v) {
    if (v<0 || v>3) throw new IllegalArgumentException("v=" + v);
    return value[v];
  }
©2009 S.Prasitjutrakul