I was given the following exercise:
"Given this class,
class Test {
int a;
Test(int a) {
this.a = a;
}
}
write a method called swap() that exchanges the contents of the objects referred by Two Test object references."
I have written three slightly different examples of the exercise:
Example 1
class Test {
public int a;
Test(int a) {
this.a = a;
}
public void swap(Test otherObject) {
int tempVar;
tempVar = this.a;
this.a = otherObject.a;
otherObject.a = tempVar;
}
}
class Chapter6_2c {
public static void main(String[] args) {
Test obj1 = new Test(1);
Test obj2 = new Test(2);
System.out.println("obj1 has value " + obj1.a);
System.out.println("obj2 has value " + obj2.a);
obj1.swap(obj2);
System.out.println("\nafter swap()\n");
System.out.println("obj1 has value " + obj1.a);
System.out.println("obj2 has value " + obj2.a);
}
}
Example 2
class Test {
public int a;
Test(int a) {
this.a = a;
}
public static void swap(Test objectOne, Test objectTwo) {
int tempVar;
tempVar = objectOne.a;
objectOne.a = objectTwo.a;
objectTwo.a = tempVar;
}
}
class Chapter6_2b {
public static void main(String[] args) {
Test obj1 = new Test(1);
Test obj2 = new Test(2);
System.out.println("obj1 has value " + obj1.a);
System.out.println("obj2 has value " + obj2.a);
Test.swap(obj1, obj2);
System.out.println("\nafter swap()\n");
System.out.println("obj1 has value " + obj1.a);
System.out.println("obj2 has value " + obj2.a);
}
}
Example 3
class Test {
int a;
Test(int a) {
this.a = a;
}
}
class Chapter6_2a {
public static void swap(Test objectOne, Test objectTwo) {
int tempVar;
tempVar = objectOne.a;
objectOne.a = objectTwo.a;
objectTwo.a = tempVar;
}
public static void main(String[] args) {
Test obj1 = new Test(1);
Test obj2 = new Test(2);
System.out.println("obj1 has value " + obj1.a);
System.out.println("obj2 has value " + obj2.a);
swap(obj1, obj2);
System.out.println("\nafter swap()\n");
System.out.println("obj1 has value " + obj1.a);
System.out.println("obj2 has value " + obj2.a);
}
}
In the examples 1 and 2 the swap() method was written as a member of the Test class, with the difference that in the example 2 is defined as static. In the example 3 the swap() method is defined inside the main() method.
My question is, which one is the best practice or most professional way to define the swap() method, from design, overhead and clarity point of view?
I do have some thoughts but I really need your opinion to confirm them as right or wrong:
Between the Examples 1 and 2 I believe that the best way is to define the swap() method as static (Example 2) from overhead point of view, because the static members of a class are not included in the instances of that class. Is my assumption correct?
The Example 3 is not a good practice to define the swap() method from design and clarity point of view, because first the swap() method is very related to the Test class and should defined as its member and second, in general, is better to define all methods outside main() in close related classes. Is this assumption also correct?
I thank you in advance for your time to help me!!!