-4

Consider a class Student. Following are 2 ways to initialize instance values in default constructor:

class Student {
     int units;

     Student() {
         this(10);
     }

     Student(int x) {
         units = x;
     }
 };

Is above way better than following:

 class Student {
     int units;

     Student() {
         units = 10;
     }

     Student(int x) {
         units = x;
     }
 };

Which way is better and preferable ?

AnujaP
  • 127
  • 1
  • 11
  • If you are asking if there is a standard practice for this, there is not. – Rabbit Guy Jan 11 '17 at 20:21
  • 1
    Possible duplicate of [Best way to handle multiple constructors in Java](http://stackoverflow.com/questions/581873/best-way-to-handle-multiple-constructors-in-java) – Tom Jan 11 '17 at 20:39

3 Answers3

3

In this simple case, we cannot see a real advantage of the one or the other one solution.

But as a general rule I prefer the solution where the constructor calls the other constructor as it avoids to repeat yourself.
Suppose in the constructor you add two or three arguments and that you perform some processings. You should probably duplicate it in the constructor without parameter.

For example with duplication in constructor:

class Student {
  int units;
  int otherUnits;
  boolean isTrue;

  Student() {
     this.units = 10;
     this.otherUnits = 20;
     this.isTrue = true; 
     computeIntermediaryValue(units,otherUnits,isTrue);
  }

  Student(int units, int otherUnits, boolean isTrue) {
     this.units = units;
     this.otherUnits = otherUnits;
     this.isTrue = isTrue;              
     computeIntermediaryValue(units,otherUnits,isTrue);         
  }
}

Undesirable duplication should be avoid.

Without duplication in constructor it looks better:

class Student {
  int units;
  int otherUnits;
  boolean isTrue;

  Student() {
     this(10, 20, true);
  }

  Student(int units, int otherUnits, boolean isTrue) {
     this.units = units;
     this.otherUnits = otherUnits;
     this.isTrue = isTrue;              
     computeIntermediaryValue(units,otherUnits,isTrue);         
  }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 1
    *"Undesirable duplication should be avoid."* Interessting sentence, especially since there are duplicates of this question available. – Tom Jan 11 '17 at 20:40
  • Good word :) Unfortunately In what you refer : stackoverflow.com/questions/581873/… the most evaluated answer brings zero explanation and I don't find any reference to code duplication either in any response. Besides in the older question the OP doesn't know how to process. It is not the case here @Tom – davidxxx Jan 11 '17 at 20:50
1

This is debatable and dependent on style.

Personally, I'd choose the first approach so there is only one primary constructor through which all the data goes at the end. In the other, secondary constructors, you can set some values by yourself (that are not injected), but all the data is in the end injected through the same constructor.

Rok Povsic
  • 4,626
  • 5
  • 37
  • 53
0

memory issue: Calling one from other constructor will cost you on memory basis. As it requires stack memory to be utilised. To be more memory efficient you should use second approach.

speed issue: In second approach compiler first has to check which constructor is going to be called and after deciding that it will call the required one. But this time taken by compiler will cost you in some situations while in first approach always default constructor is called.

user issue: Most important the way of writing code and the need of calling parameterized constructor with different parameters. Situations where user need to put customised data in constructor call, second approach seems to be quite good.

Although there is no standard practice mentioned for this but i like to use first approach.(Rest upto user)

*Duplication problem as mentioned by @davidxxx

jack jay
  • 2,493
  • 1
  • 14
  • 27