- if
super
orthis
are called, they can only be called on the first line of the constructor - an exception can be thrown if a parameter is invalid
- you should ensure that the constructed object is in a valid state
- constructors should never call an overridable method (an overridable method is one which is neither
private
,static
, norfinal)
- constructors are never
synchronized
- constructors can create thread objects, but the thread should not be started within a constructor
- constructors shouldn't pass a
this
reference to other objects - constructors can be private, in order to restrict construction
- the default constructor is the constructor provided by the system in the absence of any constructor provided by the programmer. Once a programmer supplies any constructor whatsoever, the default constructor is no longer supplied.
- a no-argument constructor, on the other hand, is a constructor provided by the programmer which takes no arguments.
Example
Resto
is an immutable class, since its state cannot change after construction. Note that:- all validation is performed in its constructor
- almost all of its javadoc is concerned with stating precisely what is to be passed to the constructor
import java.math.BigDecimal;
import java.util.Objects;
/** Model Object for a Restaurant. */
public final class Resto {
/**
Full constructor.
@param id underlying database internal identifier (optional) 1..50 characters
@param name of the restaurant (required), 2..50 characters
@param location street address of the restaurant (optional), 2..50 characters
@param average price of a meal (optional) $0.00..$100.00
@param comment on the restaurant in general (optional) 2..50 characters
*/
public Resto(
String id, String name, String location, BigDecimal price, String comment
) throws ModelCtorException {
this.id = id;
this.name = Objects.requireNonNull(name);
this.location = location;
this.price = price;
this.comment = comment;
validateState();
}
public String getId() { return id; }
public String getName() { return name; }
public String getLocation() { return location; }
public BigDecimal getPrice() { return price; }
public String getComment() { return comment; }
/** For debugging only. */
@Override public String toString(){
return id + ":" + name;
}
@Override public boolean equals(Object aThat) {
if (this == aThat) return true;
if (!(aThat instanceof Resto)) return false;
Resto that = (Resto)aThat;
for(int i = 0; i < this.getSigFields().length; ++i){
if (!Objects.equals(this.getSigFields()[i], that.getSigFields()[i])){
return false;
}
}
return true;
}
@Override public int hashCode(){
return Objects.hash(getSigFields());
}
// PRIVATE
private final String id;
private final String name;
private final String location;
private final BigDecimal price;
private final String comment;
/** Does all of the checks specified in the constructor's javadoc.*/
private void validateState() throws ModelCtorException {
//...elided
}
private Object[] getSigFields(){
return new Object[] {name, location, price, comment};
}
}