Constructors in general

Constructors: Note that a conventional distinction is made in Java between the "default constructor" and a "no-argument constructor": This distinction is necessary because the behavior of the two kinds of constructor are unrelated: a default constructor has a fixed behavior defined by Java, while the behavior of a no-argument constructor is defined by the application programmer.


Resto is an immutable class, since its state cannot change after construction. Note that:

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 { = id; = Objects.requireNonNull(name);
    this.location = location;
    this.price = price;
    this.comment = comment;
  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 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 {
  private Object[] getSigFields(){
    return new Object[] {name, location, price, comment};

See Also :
Validate method arguments
Validate state with class invariants
Initializing fields to 0 false null is redundant
Immutable objects
Private constructor
Avoid JavaBeans style of construction
Constructors shouldn't call overridables
Don't pass 'this' out of a constructor
Constructors shouldn't start threads