What is a Constructor

A constructor gives a new object the starting values it needs, and `this` tells the object's own variable apart from a parameter with the same name.

Apprentice · Java · OOP · Updated

By

It is often necessary to set initial values when you create a new instance of an object. For example, you must set the temperature and initial capacity of a refrigerator immediately after creating it, in order for your refrigerator to work as expected.

Refrigerator fridge = new Refrigerator();

fridge.setTemperature(32);
fridge.setCapacity(1500);

boolean didItemFit = fridge.addItem(30); // returns true

System.out.println("Did the item fit? " + didItemFit);

If you do not set the initial values correctly, the refrigerator would not allow you to add any items.

Refrigerator fridge = new Refrigerator();

// default capacity is 0

boolean didItemFit = fridge.addItem(30); // returns false

System.out.println("Did the item fit? " + didItemFit);

Adding a Constructor

To avoid this problem, we can add a constructor to our class. A constructor is a special function whose only purpose is to help us set the initial values of our object when we create it.

The constructor function has no return type, and the name of the function must be the exact name of the class (it is case sensitive).

public class Refrigerator
{
    private int temperature;
    private int capacity;
    private int usedSpace;

    public Refrigerator(int temperature, int capacity)
    {
        this.temperature = temperature;
        this.capacity = capacity;
        this.usedSpace = 0;
    }
}

this.temperature and temperature are 2 different variables in memory.

The keyword this refers to the current object. In other words, this.temperature refers to the object's variable named temperature. The code in the constructor that just uses the variable name temperature (without this) refers to the variable that was passed in as a parameter.

Using the Constructor

Now that the Refrigerator class has a constructor, when we create a new object, we must specify what the initial temperature and capacity of the new fridge are. Once a class has a constructor with parameters, Java no longer gives it the empty one, so new Refrigerator() stops compiling. That's the point: nobody can create a refrigerator without its starting values.

Refrigerator fridge = new Refrigerator(32, 1500);

// the capacity is 1500

boolean didItemFit = fridge.addItem(30); // returns true

System.out.println("Did the item fit? " + didItemFit);

Do it: give each cupboard its starting values

Not every kitchen cupboard is the same size: a big one holds 20 plates, and some arrive with plates already inside. Without a constructor, a new cupboard can hold 0 plates, so nothing fits until someone remembers to set it up. Give the Cupboard class a constructor, so each cupboard is made with the values it needs.

The class has two private variables, capacity (how many plates it can hold) and plates (how many it holds now). Its addPlates and describe() methods are written already: describe() returns both numbers as text, such as "6 of 20 plates".

The constructor gets two values:

  • capacity: how many plates the cupboard can hold, an int (20).
  • plates: how many plates it starts with, an int (6).

For example:

Cupboard big = new Cupboard(20, 6);
big.addPlates(10);
Code Then describe() returns Why
new Cupboard(20, 6) "6 of 20 plates" the constructor copied 20 and 6 into the cupboard
big.addPlates(10), which returns true "16 of 20 plates" 6 + 10 = 16, and 16 is 20 or fewer

Your job: write the body of the constructor Cupboard(int capacity, int plates).

  • Copy the parameter capacity into the cupboard's variable capacity.
  • Copy the parameter plates into the cupboard's variable plates.
  • Leave addPlates and describe() as they are.
  • Each cupboard keeps its own values: making a second one doesn't change the first.

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets