Question

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:

  1. 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?

  2. 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!!!

Was it helpful?

Solution

Since the Java programming language is a modern platform independent, "write once, run everywhere", object-oriented language I would use Example 1. This is because you are truly using Java as an object-oriented language as it was designed for. It takes a bit more skill also in my opinion to create a program that uses objects and object references instead of handling things all in main like an older procedural language or a simple script.

So in my opinion Example 1 is the most elegant and sophisticated. Plus, you could always extend the class and override the swap() method in a sub-class for different functionality. Overriding is not possible with static methods though. Overriding, encapsulation, and polymorphism is part of the power of object-oriented languages. Using an instance method allows some of this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top