Hello everyone
first of all thanks to daniweb and all users to help me lots of time.
now come to problem i am learning java from last one month today i wrote a program to swap two objects it is running but not performing desired task.
all instance fields are same after swaping as were earlier.

class emp{
int i,j;

emp(int i,int j)
{
this.i=i;
this.j=j;
}

void showdata()
{
System.out.println(i+"\n"+j);
}

static void sw(emp a, emp b)
{
emp temp;
temp=a;
a=b;
b=temp;
}
}



class swap{
public static void main(String arg[])
{
emp e1=new emp(10,10);
emp e2=new emp(20,20);

e1.showdata();
e2.showdata();

emp.sw(e1,e2);

e1.showdata();
e2.showdata();

}
}

please tell me wheres the problem.

Edited 5 Years Ago by aman rathi: n/a

Problem is with your sw method.
Line No 20,21,22.

In your sw method just you are swapping the reference. Not the original values.

So you can try as like the following way.

static void sw(emp a, emp b)
{
emp temp=new emp(); // To store the temp values... In this case default constructor you should define. Otherwise use two int datatype for swapping as like int i, int j.
temp.i=a.i;
temp.j=a.j
a.i=b.i;
a.j=b.j;
b.i=temp.i;
b.j=temp.j;
}
}

So your code will be

class emp{
    int i,j;
    emp(){
        
    }
    emp(int i,int j)
    {this.i=i;this.j=j;} 
    
    void showdata(){
        System.out.println(i+"\n"+j);
    } 
    
    static void sw(emp a, emp b){
       emp temp=new emp(); // To store the temp values... In this case default constructor you should define. Otherwise use tow int datatype for swapping as like int i, int j.
temp.i=a.i;
temp.j=a.j;
a.i=b.i;
a.j=b.j;
b.i=temp.i;
b.j=temp.j;

    }
}  

class swap{public static void main(String arg[]){
    emp e1=new emp(10,10);emp e2=new emp(20,20);
    e1.showdata();
    e2.showdata();
    emp.sw(e1,e2); 
    e1.showdata();e2.showdata(); 
}
}

Edited 5 Years Ago by Muralidharan.E: n/a

thank you sir i got your point.
one more question
is there any way to swap value of two objects without referring to their instance fields by using only objects. is there any more method to do this??

It is easy to swap the reference without using seperate method.

class emp{
        int i,j;
        
        emp(int i,int j){
             this.i=i;this.j=j;
        } 
    
    void showdata(){
        System.out.println(i+"\n"+j);
    } 
}
    

class swap{
    public static void main(String arg[])
{
    emp e1=new emp(10,10);emp e2=new emp(20,20);
    e1.showdata();
    e2.showdata();
    //emp.sw(e1,e2); 
    emp temp;
    temp=e1;
    e1=e2;
    e2=temp;
    e1.showdata();e2.showdata(); 
 }
}

Its perfectly working.
but what you did here is the same i did in my code the only difference is that i did it in separate class.
in your 1st post you said

In your sw method just you are swapping the reference. Not the original values.

i just want to know that whats the difference what you did in last post and in my first post.
according to your logic it should also be by reference

emp.sw(e1,e2);
static void sw(emp a, emp b)

Here in this, a and b are pointing to the location where e1 and e2 are pointing respectively.

So the changes(swapping) if you will make on a and b without touching its fields that wont affect the e1 and e2.

Have you got it now?

This question has already been answered. Start a new discussion instead.