amp
amp

Reputation: 12352

Initialization and modification of an ArrayList

Due to my few concepts of Java, I have a basic question.

In this situation:

Device devA = new Device();
Device devB = new Device();
ArrayList<Device> allDev = new ArrayList();
allDev.add(devA);
allDev.add(devB);

If after that I modify devA or devB, also allDev will be modified accordingly?

Upvotes: 0

Views: 118

Answers (2)

uaarkoti
uaarkoti

Reputation: 3657

In Java when you create a new Object you are getting a reference/handle to the object and anytime you have a handle to the object and making changes to the object, anyone having the same reference will be able to see those changes.

So the answer to your question is yes because you are in essence making changes to the same object.

Upvotes: 2

amit
amit

Reputation: 178491

Yes. You add a reference to devA and devB to the ArrayList. Any change to these object will be reflected to them when you access them through the ArrayList as well.

Upvotes: 5

Related Questions