0

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?

2
  • 3
    I'm not trying to be sarcastic or anything but why not try it out in code? Things like this are better learned that way. If you are still having trouble that way then come back here, post your code and the trouble you are having. You need to help yourself before others can try to help you. Commented Mar 22, 2012 at 23:23
  • @Pete, I understand your opinion and next time I will try before ask :) Commented Mar 22, 2012 at 23:34

2 Answers 2

5

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.

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

Comments

2

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.

Comments

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.