Please enable JavaScript to use CodeHS

Chapter 5

Writing Classes

5.2 Constructors

Constructors are essentially object creators. You use constructors to create new objects using a class. When you create new objects using constructors, you also set any initial values of instance variables if necessary. These initial values of instance variables can be set either with or without the use of parameters.

State of an Object

An object is said to have state and behaviors. The state of an object represents the attributes and their values at a given time and is defined by instance variables belonging to the object. These instance variables create a “has-a” relationship. For example, if you think about a Rectangle object, you can say that the Rectangle has a length and the Rectangle has a width. At any given point in time, the state of the rectangle can be defined by the length and width of the Rectangle.

Constructors are used to set the initial state of an object, which should include initial values for all instance variables.

The Constructor

As you saw back in unit 2, the constructor is used to instantiate a new object and is called anytime an object is created. Constructors can serve multiple purposes, but one purpose is to set the initial state of variables. Take a look at the example showing how you would write a constructor:

public class Dog
{
    // Instance variables
    private String name;
    private int age;
    private double weight;
    private boolean isAlive;

    // Constructor
    public Dog(String theName, int theAge, double theWeight, boolean isThisAlive)
    {
        // Instance variable initialization
        name = theName;
        age = theAge;
        weight = theWeight;
        isAlive = isThisAlive;
    }
}
Plain text

The constructor parameters are local to the constructor only. Because of this, you will notice how the local constructor parameters are used inside the constructor to set the initial value of the instance variables. Additionally, it is important to note that when a mutable object is passed as a parameter, the instance variable is initialized with a copy of the variable. As a result, any modifications to the instance variable will not be reflected in the original object.

No-Argument Constructor

While a constructor does play an important role, it is possible to create a class without a constructor. When no constructor is provided, Java provides what is known as a no-argument constructor. The no-argument constructor executes anytime an object is created and sets any instant variable to their default value.

Example:

public class Dog
{
    private String name;
    private int age;
    private double weight;
    private boolean isAlive;
}
Plain text

In this example, when a Dog object is created, the instance variables will be set to their default values, which is zero for the integer and double, an empty string for name, and false for the Boolean value.

SuperHero Class

Initializing an Object without a Constructor

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    An object’s state is defined by the object’s

Exercise: Dog Class

For this exercise, you are going to complete the Dog class. The structure has been set up for you, but you will need to complete two constructors (overloaded).

The first will take three parameters and assign the values to the instance variables. The method signature should take the name, then age, and breed.

The second constructor will only take name then age. For this one, you will assign a value of unknown to the dog’s breed.

Once complete, create and print two Dog objects in the DogTester class. The first should use three parameters and the second should only use two parameters.

Sample Output:

Name: Karel
Age: 7
Breed: Retriever

Name: Mont
Age: 1
Breed: Unknown
Plain text