This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes swapping object thru a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "swapping object thru a method" Watch "swapping object thru a method" New topic
Author

swapping object thru a method

Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
hi
I want to swap two object through a method. I tried to do something as given below(I have written down a simplfied code here):
<code>
class myclass{
int x;
int y;
String s;
myclass(int x,int y,String s)
{
this.x=x;this.y=y;this.s=s;
}
}
public class test{
public static void main(string []args)
{
myclass obj1=new myclass(1,2,"first");
myclass obj2=new myclass(3,4,"Second");
system.out.println(obj2.s);
system.out.println(obj1.s);
swap(obj1,obj2);
system.out.println(obj1.s);
system.out.println(obj2.s);
}
void swap(myclass o1,myclass o20
{
myclass temp=o1;
o1=o2;
o2=temp;
}
}
</code>
But ofcourse, the objects are not swapped in when we return to main method from swap.This was confusing because we learnt that in java objects are passed by reference. after thinking for sometime, I have come to a conclusion. I dunno if it is the right answer, but this is what I think:
In java, value held by the object reference (i.e. the address of the object it points to) is actually passed , which is accepted by a another object reference.
In the swap method we get a object refrence o1 which points to the same object pointed(referenced) by obj1. Similarly o2 point to object pointed by obj2.When we swap, we are actually pointing o1 to object pointed by o2(obj2), and pointing o2 to object pointed by o1(obj1).But obj1 and obj2 continue top point to what they pointed before calling swap.
Is what I think correct?
So how do I swap two object through a method and get it swapped when I return from that method.
regards
Tanveer


Author of JPhotoBrush Pro (www.jphotobrushpro.com)
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Tanveer,
The Java Tutorial states,

In Java methods, arguments are passed by value. When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object.

Therefore, when you call the swap() method you're passing in two reference type. Since "the method cannot change the object reference", your swap routine has no affect. You can however swap the contents.

-Peter
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
thanx. But suppose I have a swap method which swaps any two objects, swap(Object o1,Object o2). I want to swap any two objects thru the method. Then what do I do? At compile time I won't know what is the object class.
regards
Tanveer Rameez
------------------
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Maybe the method should really be
swap(Object[] items)
then you can just swap the positions in the array.
It all seems a bit pointless, though. Do you actually have a need for any of this?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Frank's suggestion is very good and it's how the JDK implements the swap() rountine in the lower level implementation of the java.util.Collections.sort() method.

-Peter
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
thanx for the suggestions. I think swapping thru an array is a good solution.
regards
Tanveer
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: swapping object thru a method