4

I'm trying to make a list containing different objects.

List<Object> list = new ArrayList<Object>();
    defObject defObj;
    optObject optObj;

and defObject has just one property for a String.

    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 multiple defObjects to the list and go through the list after I'm done adding the element they all contain the same string, which was of the last defObject added to the list.

I'm doing something like this to add the 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 a string of "y"

Please help me figure out why the objects aren't being added correctly and the properties are all same as the last defObj added to the list.

Mitciv
  • 813
  • 3
  • 15
  • 21

4 Answers4

12

The problem is defObj is static so all instances are sharing the same variable. Remove the word static from everywhere in your class and everything will likely work as you expect.

Asaph
  • 159,146
  • 25
  • 197
  • 199
3

The String defObj variable is static, so it's always equal for all instances of defObject. Remove the "static" before your method and attribute declaration and it should work.

Marc Müller
  • 717
  • 6
  • 15
3

Replace:

public static class defObject
{
    public static String defObj;
    ...

With:

public static class defObject
{
    public String defObj;
 ....

Or even better for:

public class DefObject {
    private String defObj;
    ....

Using the keyword static will make the attribute or method a class method, which means there will be only one for all the instances.

Remove it from your code. Also notice in Java by convention the class name starts with uppercase and the opening brace is in the same line.

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 1
    I'll give you class names start with uppercase but def not opening brace should be on the same line -- that one is highly subjective. – non sequitor Oct 26 '09 at 05:04
  • A lot of developers put the opening brace on its own line. – Jim Ferrans Oct 26 '09 at 05:28
  • @non sequitor: Yes, it is highly subjective, but it turns out developers whose primary programming language is Java do that. I would rather read code that looks like the one you're targeting in this case Java. I do the same with C#, I use opening brace in it's own line, because that's what most C# developers do. – OscarRyz Oct 26 '09 at 16:26
0

After you remove static from public static String defObj; and make it private you will also need to remove static from your method signatures as static methods can't access instance variables from a static context i.e. defObject.getDefObj() can't access the instance variable defObj since the compiler can't ensure that it already exists - no instance was instantiated and so no instance variable. This can only be done with static properties on class load.

non sequitor
  • 18,296
  • 9
  • 45
  • 64