Assigning Object Reference Variables
By: aathishankaran
Object reference variables act differently than you might expect when an assignment takes place. It has been explained in this article. You can take an example of following code. Here an object t1 is created for Test class and another object T2 is assigned for the t1 then what do you think the following fragment does?
Test t1 = new test ();
Test t2 = t2;
You might think that t2 is being assigned a reference to a copy of the object referred to by t1. That is, you might think that t1 and t2 separate and distinct objects. However, this would be wrong. Instead, after this fragment executes, t1 and t2 will both refer to the same object. The assignment of t1 to t2 did not allocate any memory or copy and part of the original object. It simply makes t2 refer to the same object, as does t1. Thus, any changes made to the object through t2 will affect the object to which t1 is referring, since they are the same object.
This situation is depicted here:
|
|
|
||||
Test Object |
||||||
|
|
Although t1 and t2 both refer to the same object, they are not linked in any other way. For example, a subsequent assignment to t1 will simply unhook t1 from the original object without affecting the object or affecting t2.
For example,
Test t1 = new test ();
Test t2 = t1;
//…
t1 = null
Here, t1 has been set to null, but still points to the original object.
Archived Comments
1. I see two mutually incompatible statements in the article above. Can someone please explain?
View Tutorial By: Deepak Mohan at 2015-12-22 01:50:29
2. It is clear but at the starting code there is a written mistake I thing i.e.-
Test t1 =
View Tutorial By: Kiranmoy Pradhan at 2015-08-20 21:55:15
3. Thanks, It's exactly. A my friends has the same issue.
View Tutorial By: Slim at 2012-09-29 02:52:12
4. i have asked for refercing the object of main class in any other class like in a j2se database proje
View Tutorial By: deepesh at 2012-08-20 10:33:58
5. This was simple and clear. Thanks!
View Tutorial By: Evik James at 2011-08-02 12:17:54
6. finally i got it !!! thanks a lot!!! you really helped me
View Tutorial By: maher at 2010-11-11 02:44:07
7. its not solve my problem.i.e
the concept is not clear
View Tutorial By: murli at 2010-10-02 23:23:11
8. Thanks, cleared the situation!!
View Tutorial By: alex at 2010-03-23 16:16:49
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program