I am trying to create a list containing different objects.
List<Object> list = new ArrayList<Object>(); defObject defObj; optObject optObj;
and defObject has only one property per row.
public static class defObject { public static String defObj; public defObject(String x) { setDefObj(x); } public static String getDefObj() { return defObj; } public static void setDefObj(String defObj) { defObject.defObj = defObj; } }
if I add several defObjects to the list and go through the list after I finish adding the item, they all contain the same line that was from the last defObject added to the list.
I am doing something like this to add objects to the list:
if (whatever) list.add(defObj = new defObject("x")); else if(whatever) list.add(defObj = new defObject("y"));
and the result is two defObjects with the string "y"
Please help me understand why the objects are not being added correctly, and the properties are all the same as the last defObj added to the list.
source share