File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes passing references to methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing references to methods" Watch "passing references to methods" New topic
Author

passing references to methods

cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
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
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
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
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]
adam faith
Greenhorn

Joined: Aug 24, 2002
Posts: 22
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing references to methods