Constructors are not used for readability. In fact, I cannot think an example in which a constructor could be user for readability.
The constructor in the example just gives an initial value to class members.
The class has two constructors:
- One constructor doesn't receive any parameters. This constructors randomly generates a value for each member by calling the
roll()
method.
- The other constructor allows you to set the initial value of the class members.
You are right when you say the class could work without constructors, making it clear that an empty constructor is implied (inherited from Object
), so in reality the class will have at least one an empty constructor.
But... as the following no-constructor alternatives show, one could soon reach to the conclusion that providing constructors makes the class more usable:
Constructor-less example 1.
Here, to ensure members have a valid state(*), they where initialized on declaration with a value of one:
public class PairOfDice {
public int die1 = 1;
public int die2 = 1;
public void roll() {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
}
Constructor-less example 2.
Here, in addition to what was done above, setters were provided to change the value of members arbitrarilly, but then it would be possible to change the values after roll, which is not desirable.
public class PairOfDice {
public int die1=1;
public int die2=1;
public void setDieOneValue(int i){die1=i;};
public void setDieTwoValue(int i){die2=i;};
public void roll() {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
}
Constructor-less example 3.
Here members values are randomly generated on initialization. Notice repeated code.
public class PairOfDice {
public int die1=(int)(Math.random()*6) + 1;
public int die2=(int)(Math.random()*6) + 1;
public void roll() {
die1 = (int)(Math.random()*6) + 1;
die2 = (int)(Math.random()*6) + 1;
}
}
(*) by valid state I mean that the members have values that enforce the business rules, or runtime errors. For example, die values should be in the range 1..6 so one should garantee that they don't have zero value at initialization. A business rule is a rule imposed by the "business". The "bunisess" or dice rolling says that die have faces from one to six (I know theere are ther kind of dices but for the sake of simplicity we will leave them out). Having a dice throw a value of 0 would probably make the program fail. That's what is meant by valid state.