I am trying to assign some values to static class and it does not work as I expected. I was expecting to see different values for different classes. However, they fall into the same value because it is static??? See code snippet here:

    NewOperationResponseSimpleType[] resp=new NewOperationResponseSimpleType[3];
    NewOperationResponseSimpleType[] res1=new NewOperationResponseSimpleType[3];

    NewOperationResponseSimpleType[] res1=new NewOperationResponseSimpleType[3];
    NewOperationResponseSimpleType[] res2=new NewOperationResponseSimpleType[3];
    NewOperationResponseSimpleType[] res3=new NewOperationResponseSimpleType[3];

    for (int i=0; i<3;i++){
        res1[i]=NewOperationResponseSimpleType.resp1;
        res1[i]._value_=i+"_Value1";
        System.out.println("res1["+i+"].getValue()"+res1[i].getValue());

        res2[i]=NewOperationResponseSimpleType.resp2;
        res2[i]._value_=i+"_Value2";
        System.out.println("res2["+i+"].getValue()"+res2[i].getValue());

        res3[i]=NewOperationResponseSimpleType.resp3;
        res3[i]._value_=i+"_Value3";
        System.out.println("res3["+i+"].getValue()"+res3[i].getValue());

        resp[i].resp1=res1[i];
        System.out.println("resp["+i+"].resp1.getValue()"+resp[i].resp1.getValue());
        resp[i].resp2=res2[i];
        System.out.println("resp["+i+"].resp2.getValue()"+resp[i].resp2.getValue());
        resp[i].resp3=res3[i];
        System.out.println("resp["+i+"].resp3.getValue()"+resp[i].resp3.getValue());


    }

    for (int j=0; j<3; j++){

        System.out.println("resp["+j+"].resp1.getValue()"+resp[j].resp1.getValue());
        System.out.println("resp["+j+"].resp2.getValue()"+resp[j].resp2.getValue());
        System.out.println("resp["+j+"].resp3.getValue()"+resp[j].resp3.getValue());
    }



public class NewOperationResponseSimpleType  {

    public java.lang.String _value_;
    public  java.util.HashMap _table_ = new java.util.HashMap();

    // Constructor
    public NewOperationResponseSimpleType(java.lang.String value) {
        _value_ = value;
        _table_.put(_value_,this);
    };

    public static  final java.lang.String _resp1 = "resp1";
    public static  final java.lang.String _resp2 = "resp2";
    public  static final java.lang.String _resp3 = "resp3";
    public  static  NewOperationResponseSimpleType resp1 = new NewOperationResponseSimpleType(_resp1);
    public  static  NewOperationResponseSimpleType resp2 = new NewOperationResponseSimpleType(_resp2);
    public  static  NewOperationResponseSimpleType resp3 = new NewOperationResponseSimpleType(_resp3);
    public java.lang.String getValue() { return _value_;}
    public  NewOperationResponseSimpleType fromValue(java.lang.String value)
          throws java.lang.IllegalArgumentException {
        NewOperationResponseSimpleType enumeration = (NewOperationResponseSimpleType)
            _table_.get(value);
        if (enumeration==null) throw new java.lang.IllegalArgumentException();
        return enumeration;
    }
    public  NewOperationResponseSimpleType fromString(java.lang.String value)
          throws java.lang.IllegalArgumentException {
        return fromValue(value);
    }
    public boolean equals(java.lang.Object obj) {return (obj == this);}
    public int hashCode() { return toString().hashCode();}
    public java.lang.String toString() { return _value_;}


}

Dani AI

Generated

As found, the behavior comes from using static singleton instances. Assigning res1[i] = NewOperationResponseSimpleType.resp1 copies the reference to the same single object into every slot; mutating that object's _value_ changes that one instance and is visible everywhere. 's note about static state being shared is the right diagnosis. The correct fix depends on intent: either create distinct value instances for each array element, or treat the class as an immutable enumeration.

If the goal is distinct objects with different values, construct new instances for each slot instead of reusing the static ones:

for (int i = 0; i < 3; i++) {
    res1[i] = new NewOperationResponseSimpleType(i + "_Value1");
    res2[i] = new NewOperationResponseSimpleType(i + "_Value2");
    res3[i] = new NewOperationResponseSimpleType(i + "_Value3");
}

If the class is meant to represent a fixed set of named constants, prefer a proper enum (immutable, safer):

public enum NewOperationResponseSimpleType {
  RESP1("resp1"), RESP2("resp2"), RESP3("resp3");

  private final String value;
  private static final Map<String, NewOperationResponseSimpleType> TABLE = new HashMap<>();
  static { for (NewOperationResponseSimpleType e : values()) TABLE.put(e.value, e); }

  private NewOperationResponseSimpleType(String v) { value = v; }
  public String getValue() { return value; }
  public static NewOperationResponseSimpleType fromValue(String v) { return TABLE.get(v); }
}

Practical checks and cautions: verify whether array elements are initialized before use (the snippet shows duplicate res1 declarations and possible uninitialized resp[] slots), make _value_ private (and final for enums) to prevent accidental mutation, and make any lookup map static when implementing a type-safe enum. To confirm whether two variables reference the same object, print res1[0] == res1[1] or System.identityHashCode(obj).

I don't see any static classes here, Do you mean static variables?
Why are you using static variables?

I was expecting to see different values

You only get different values (one per class instance) with non static variables.
With static variables all instances of a class share one variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.