473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

basic java

When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost?
Jul 13 '07 #1
3 1282
r035198x
13,262 8TB
When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost?
Java passes by value always
Jul 13 '07 #2
itsraghz
127 100+
When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost?
As the admin said, "Java passes everything by value". This everything includes even the references to objects.

Lets say, you have a method which takes an object as a parameter as follows

Expand|Select|Wrap|Line Numbers
  1. public void doChange(MyClass myClassObj)
  2. {
  3. //do something..
  4. }
  5.  
And you call that method from some other piece of code as follows

Expand|Select|Wrap|Line Numbers
  1. //calling method
  2. public void method1()
  3. {
  4. MyClass myClassObj = new MyClass();
  5. doChange(myClassObj);
  6. }
  7.  
The reference variable myClassObj is being assigned to an object of class MyClass in Heap. Just like primitive variables holding bit patterns to represent a value, reference variables also store the bit patterns to reach an object.

Once you call the method doChange() by passing the reference variable "myClassObj" to it, the value of the bit patterns held by "myClassObj" is being copied and passed into the called method doChange(). In the called method doChange(), the copied-and-sent bit pattern is received in the same name as that of the original reference variable "myClassObj". But the compiler treats that as a different one just like primitives.

Lets say the received reference variable as "myClassObjLocal" for easy understanding. In this stage, both the original refernece variable "myClassObj" and the received reference variable inside the doChange() method "myClassObjLocal" (to compiler) point to the same object in Heap.

Now if you change the state of the object being pointed by, it will reflect to both the reference variables because both of them point to the same object in Heap. Whereas,if you change the received reference variable "myClassObjLocal" to point to a new object (reassign a different object), it does NOT reflect back to the original object because the bit pattern to the "myClassObjLocal" alone gets changed!

If you are clear with the above paragraphs, lets go to an example to make it more clear.

Lets take a small example.

Expand|Select|Wrap|Line Numbers
  1. class TestObjRef
  2. {
  3. int intValue;
  4.  
  5. public TestObjRef()
  6. {
  7. intValue = 1;
  8. }
  9.  
  10. public static void changeStateOfObject(TestObjRef obj)
  11. {
  12. obj.intValue = 2;
  13. }
  14.  
  15. public static void changeReference(TestObjRef obj)
  16. {
  17. obj = new TestObjRef();
  18. obj.intValue = 9;
  19. }
  20.  
  21. public static void main(String[] args)
  22. {
  23. TestObjRef obj1 = new TestObjRef();
  24. System.out.println("obj1.intValue (1) = "+obj1.intValue);
  25. changeStateOfObject(obj1);
  26. System.out.println("obj1.intValue (2) = "+obj1.intValue);
  27. changeReference(obj1);
  28. System.out.println("obj1.intValue (3) = "+obj1.intValue);
  29. }
  30. }
  31.  
Running the above code produces the following output:

Expand|Select|Wrap|Line Numbers
  1. obj1.intValue (1) = 1
  2. obj1.intValue (2) = 2
  3. obj1.intValue (3) = 2
  4.  
changeStateOfObject Method

This is because, changeStateOfObject() method just changes the value of the variable "intValue" which definitely constitutes the state of object. And as such, both the "obj1" in main() method and "obj" in changeStateOfObject() method point to the same object. Means, they both hold the same bit patterns to reach a single object of TestObjRef class in heap.

That's why the output in the second line shows the changed state of object "2" as the value of "intValue" property.

changeReference() Method

If you look at the changeReference() method, by the time of receiving the argument, both the "obj1" reference variable in main() method and received reference variable "obj" in changeReference() method both hold the same bit patterns to reach the same and single TestObjRef class in Heap.

But inside the method, the received "obj" reference variable is reassigned to a newly created object of TestObjRef class. In this case, only the bit pattern of the "obj" reference variable inside the changeReference() method is changed and the change is NOT reflected back to the originally sent reference variable "obj1".

That's why the third line in the output still shows the value of "intValue" as "2" since the changed value "9" is only reflected in the received local reference variable "obj" in changeReference() method.

Hope this helps.

Note
The received argument (object reference variable) is treated local to the method. That means, the scope of the variable is only local to the called method just like primitives and the variable cannot be accessed outside the method.
Jul 13 '07 #3
itsraghz
127 100+
When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost?
Adding to the previous example, you can have a look at this link for another example of same topic with explanation.
Jul 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark R Rivet | last post by:
I've installed JDK1.1.5 on my windows 2000 professional system and when I give it the command ( in a command prompt window) to compile, it does, but when I try to run the program I get this...
5
by: Lee David | last post by:
I went to the sun site and downloaded what I hope is the development part of java. I downloaded JDK5 with Netbeans. I installed it and now have a folder in my program group "Netbeans". Is that...
14
by: luis | last post by:
Are basic types (int, long, ...) objetcs or not? I read that in C# all are objects including basic types, derived from Object class. Then in msdn documentation says that boxing converts basic...
1
by: Andrea Temporin | last post by:
Is there someone who can tell me if it's possible to call Java Beans from Visual Basic Applications (windows forms application I mean)? Can I Use J#? Is It possible to create a Java web services as...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
28
by: grappletech | last post by:
I took Pascal and BASIC in a couple of beginner programming courses about a decade ago and did well with them. I am good with pseudocode, algorithms, and the mathematics of programming. I decided...
6
by: John Bailo | last post by:
http://www.informationweek.com/software/showArticle.jhtml?articleID=196600515 Developers Embrace Java, Drop Visual Basic "Developers have abandoned Microsoft's Visual Basic in droves during...
6
by: Simon Walsh | last post by:
I'm an Electronics student in college and I'm currently working on a project. I was given a circuit diagram for my project, from which I had to design a printed circuit board to be sent off and...
43
by: Bill H | last post by:
25 years ago every computer came with some form of Basic interpreter so you could use yoru computer without having to buy more software. Is Javascript (teamed with HTML) set to become the new...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.