• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

swapping object thru a method

 
Tanveer Rameez
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Tran
Bartender
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peter Tran
Bartender
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx for the suggestions. I think swapping thru an array is a good solution.
regards
Tanveer
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic