Wednesday, 7 October 2015

Java Program - Passing values to methords


public class Passing {

public static void main(String[] args) {
// TODO Auto-generated method stub
Demo obj1 = new Demo(10);
Demo obj2 = new Demo(20);
Demo obj3 = new Demo(30);
System.out.println("The values before calling swap " +obj1.i + "\t" +obj2.i);
obj3.swap(obj1.i, obj2.i);
System.out.println("The values after calling swap " +obj1.i + "\t"  +obj2.i);
}

}


public class Demo {
int i;
Demo(int i){
this.i=i;
}

void swap(int i,int j){
int temp;
temp=i;
i=j;
j=temp;
}
}

Output:-
The values before calling swap 10 20
The values after calling swap 10 20

No comments:

Post a Comment