Forums Register Login

passing references to methods

+Pie Number of slices to send: Send
Hi all
I have the following code and try to test when references are passed to objects when invoked,but get different output.
When i create one object and want to print out the results i get the results of the method,but when i create another object and pass that objects ref into the method it gives me the output of the first object with the changes.
Can someone please give me clarity on the following code please.
class Record
{
int num;
String name;
public static void testMethod(Record r)
{r.num = 100;
r.name = "Fred";
}
}

public class VariableTest //extends A
{
public static void main(String args[])
{
Record id = new Record();
Record id1 = new Record();
id.num = 100;
id.name = "cornel";
id.testMethod(id1);//here i get different output,but when ref is
//id gives me the result of method
System.out.println(id.name+" " +id.num);
}
}
Thanks for any advice
cornel
+Pie Number of slices to send: Send
If I understand you correctly, you are comparing the result of applying testMethod first on id and then on id1.
But in both cases you are printing id.name and id.num. If when you give id1 as an argument to testMethod, you also print id1.name and id1.num, you will see you get the changed values.
-Barry
[ September 08, 2002: Message edited by: Barry Gaunt ]
+Pie Number of slices to send: Send
Hi again
Thanks for the quick reply
What i want to establish is what happen when you changing values in the method like the code i put in.Now when you invoke the method with id by creating one object id.name and id.num prints the values of the method not the values modified by id
But when you create another object and pass id1 into the method it gives you the output of id modified.
This is actually what confuses me.
cornel
+Pie Number of slices to send: Send
But what I see is always the new values after a call to testMethod. testMethod will always change the fields of whatever Record instance that you pass to it to 100 and "Fred" respectively. Can I suggest that you change num to 101 as well as name to "Fred" in the testMethod code?
Can I also suggest that you get hold of free BlueJ, with this little tool you can see what's going on.
-Barry
[ September 08, 2002: Message edited by: Barry Gaunt ]
[ September 08, 2002: Message edited by: Barry Gaunt ]
+Pie Number of slices to send: Send
Cornel,
When you call your test method with the reference, you are modifying the variables to what is in the method block, hence when you do the print statement, you will get what the values as per your method block.
The fact that you changed the values before you called the method by specific id.num, doesnt matter, because in your test method you are taking the object reference id and once again changing the contents of that object.
The key point to note is that unlike primitve variables, refrenece variables will allow you to modify the contents of the object, but in primitives the original is untouched.
Hope this helps
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 682 times.
Similar Threads
Getting duplicate ID
Help using threads
instance method/static context/dynamic binding
Question on syncrhonized methods
keyword "new"
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:34:00.