LuckySlevin
LuckySlevin

Reputation: 745

Having different ArrayLists for class objects in Java

I have a class and this class has one public element ArrayList myList.(I have other elements too in my class but they have nothing to do with this element)

What I am trying to do is having different ArrayLists for different Class Objects. However when I tried to code this, even if I use different class objects, the code reserves every entry in one single ArrayList. What am i doing wrong?

This is what i tried: My class:

public class myClass {

public static ArrayList myList;
public static ArrayList getList() {
    return myList;
}
public static void setList(ArrayList myList) {
    myClass.myList = myList;
}

In main:

 myClass my = new myClass();
 myClass my2 = new myClass();
 ArrayList tmp = new ArrayList();
 ArrayList tmp2 = new ArrayList();
 tmp.add("aaaaa");
 tmp.add("bbbbb");
 tmp2.add("ccccc");
 tmp2.add("ddddd");
 my.setList(tmp);
 my2.setList(tmp2);
 for(int i=0;i<my.getList().size();i++)
 {
 System.out.println(my.getList().get(i));
 }

And the output of this main is being:

ccccc
ddddd

but i want it to be:

aaaaa
bbbbb

What am i doing wrong?

Upvotes: 0

Views: 568

Answers (4)

Lion
Lion

Reputation: 19027

Modify your class as follows

public class myClass {

    public ArrayList myList;
    public ArrayList getList() {
    return myList;
}
public void setList(ArrayList myList) {
    listler.myList = myList;
}

You're using static ArrayList myList which is common to all the objects of your class myClass. Therefore, you're not getting what you expect. Avoid using static, if you need to use the ArrayList myList separately to all the objects of the class.


Java allows you to access static members of a class in both the ways with its objects and with associating them with their class name but its a bad practice to associate static members with the class objects.


Associating static members themselves with the class object was actual a flaw in language design itself (hence, should be avoided accessing them with objects) which was corrected by C# and hence in C# you can not associate static members with the respective class objects.


In java, some IDEs might issue warnings or errors, if you try to access static members with the respective class objects.

Upvotes: 0

Duncan
Duncan

Reputation: 763

As identified by the other comments, you are declaring the ArrayList as static, which means it is shared across all object instances of the class - as such in the following statement:

my.setList(tmp);
my2.setList(tmp2);

...the second setList overwrites the first.

Read on up the Singleton pattern for a good example of where to use static fields:

Singleton pattern

As an aside, consider passing your values into the constructor and only providing a getter, thus encapsulating the use of ArrayList and making the values immutable - for example:

myClass my = new myClass("aaaa", "bbbb");
String element = my.get(0);

Finally, if using Java 5 or 6, think about using Generics when working with collections/lists:

List<String> tmp = new ArrayList<String>();

Hope that helps.

Upvotes: 0

soulcheck
soulcheck

Reputation: 36767

You declared myClass#myList static, which means only one instance of myList will exist, inside the CLASS myClass. Declare it non-static, including the getter and setter, to have different instances for my and my2 and you're good to go.

Upvotes: 3

Sid Malani
Sid Malani

Reputation: 2116

you are using static in your class.

remove static keyword for the variable in your class and you should be fine. Static is class scoped so will be shared across objects.

Upvotes: 0

Related Questions