1

Suppose I have this code.

public class Data {
    public static int x, y;
    // ...
}
// ...
public static ArrayList<Data> myData = new ArrayList<Data>();

Now, if from my main class do

Data thisData;
thisData.initData();
myData.add(thisData);
thisData.changeData();
myData.add(thisData);
// ... repeat and repeat and repeat

The question is: how does the static prefix change the behaviour of the code? I mean: will myData be populated by always the same object, because its fields x and y have been declared static? Or will myData be populated by always new objects? In other words: how does the add() statement add the object? duplicating it?

thanks.

5 Answers 5

3

It doesn't duplicate it. It simply adds the reference of the object to the list.

When you write

Data thisData = ...

thisData is a reference to the Data object you've created. Using that reference will always point to the instantiated object unless you explicity copy this object (via a copy constructor, a clone() method etc.)

So repeatedly adding thisData to a list merely means that you have multiple lists referencing the same object (or a list multiply referencing the same object). You'll see this if you change a member of thisData. Each list will reflect this change.

Note this applies to objects. For primitives (int, long etc.) values are copied. For immutable objects (objects that can't be changed) it doesn't matter, of course.

Sign up to request clarification or add additional context in comments.

2 Comments

This means that the arraylist in fact does not work? It will contain multiple references of the same static object?
You say that primitives are copied. Isn't it the case of my fields x and y?
1

Now your List is shared among instances of your object , with the static

Any instance calling .add(thisData); will add Data to your List

Comments

1

All instances of your data array will share the same fields x and y. Any attempt to modify will propagate to all newly created instances. Why to have multiple instances in array, then?

Comments

1

It will put an object which is referenced by you static variables in list. So if you don't change you static variables it will put the same objects always, if you are creating new object referenced by static variables it will add new. But be careful, if you change some fields of object with some setters that will manifest on the object already added in list.

Comments

1

static means that if any instance of your class access myData or if you access it as <Classname>.myData, you would be talking to the same ArrayList, i.e it is shared among all instances of your class.

It has nothing to do with what is added to it.

Edit: The static x,y totally eluded me. In this case, all instances of Data will share the two variables, i.e their value will be identical.

This does not mean that they are the same instance simply because the == operation will return false. Also any other variables of Data will be unique to each instance as usual.

2 Comments

Yes, this about myData being static, but the question was about the behaviour of the static fields, x and y.
@BeppiMenozzi Ah that totally escaped me, I have edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.