Assigning Object Reference Variable Java Help

Assigning Object Reference Variable

Object reference variables act differently than you might expect when an assignment , takes place. For example, what do you think the following fragment docs?You might think that  is being assigned a reference to a copy of the object referred to by That is, you might think that  to separate and distinct objects. However, this would be wrong. Instead, after this fragment executes, bi and  will both refer to the same object. The assignment of bi to  did not allocate any memory or copy any part of the original object. It simply makes  refer to the same object as does bl. Thus, any changes made to the object through  will affect the object to which bi is referring, since they are the same object. This situation is depicted here. Although  and both refer to the same object, they are not linked in any other way. For example, a subsequent assignment to  will simply unhook b1 from ‘the original object without affecting the object or affecting.

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 17, 2014 in Introducing Classes

Share the Story

About the Author

Back to Top
Share This