Demo class

public class Demo
{
    public static void main(String[] args)
    {
        /* code not yet run */ = new Coordinate2D(5, 6);
        /* more code not yet run */
    }
}

Execution of the main method remains paused while the Coordinate2D constructor executes.

Coordinate2D class

public class Coordinate2D
{
    private int x, y;

    // Other constructor not shown

    public Coordinate2D(int initX, int initY)
    {
        x = initX;
        y = initY;
        /* paused here */
    }

    // Additional methods not shown
}

The purpose of a constructor is to set the initial state of the object. The state of an object is the values of its instance variables.

At the position labeled /* paused here */, the values of the instance variables x and y are the same as the values of the parameters initX and initY, respectively.

Although it is common for the values of instance variables to be set directly to the values of parameters, this is not always the case. See Class writing order for additional discussion.

Memory diagram

initX with value 5 and initY with value 6. Box containing x with value 5 and y with value 6.

Parameters initX and initY continue to exist until the end of the constructor.

Forward to Step 3
Back to Step 1
Back to main example