This is a step by step trace of the code presented at Constructing objects.

The same trace is available with each step on its own page, starting with Step 1.

Step 1

Demo class

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

Java executes the code to the right of the assignment operator (=) first.

The code new Coordinate2D(5, 6) instantiates (constructs a new object/instance of) the Coordinate2D class.

Instantiating a class automatically calls one of the class’s constructors. In this case, the constructor with 2 parameters (shown below) is called. The values 5 and 6 are passed as arguments (sometimes called actual parameters).

All arguments in Java are passed by value. See Primitive types vs references exercises with method calls for more details.

Coordinate2D class

public class Coordinate2D
{
    private int x, y;

    // Other constructor not shown

    public Coordinate2D(int initX, int initY)
    {
        /* paused here */
        /* code not yet run */
    }

    // Additional methods not shown
}

The Coordinate2D constructor shown has 2 formal parameters initX and initY, both of type int. The values of the formal parameters are set to the values passed as arguments (5 and 6).

At the position labeled /* paused here */, the values of the instance variables x and y are both 0. Instance variables of type int default to 0.

Memory diagram

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

The diagram represents the object/instance of Coordinate2D as a box. Each object/instance of type Coordinate2D has its own copy of the instance variables x and y, shown inside the box.

The variables initX and initY are parameters. The scope of parameters (where they exist) is the method in which they are declared (in this case, a constructor).

Step 2

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.

Step 3

Demo class

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

The Coordinate2D constructor finishes executing. The new keyword returns the memory address of the newly created object/instance.

The left side of assignment operator executes next. The code Coordinate2D c1 creates a variable of type Coordinate2D named c1.

The assignment operator (=) sets the value of c1 to the memory address of the newly created object.

Memory diagram

c1 pointing to a box containing x with value 5 and y with value 6

The memory address of the object is represented as an arrow. It is common to say that c1 refers/points to the object.

Parameters initX and initY no longer exist, since the constructor is no longer executing.

Step 4

Demo class

public class Demo
{
    public static void main(String[] args)
    {
        Coordinate2D c1 = new Coordinate2D(5, 6);
        System.out.println(c1);
        /* paused here */
    }
}

When c1 is printed, Java checks if c1 points to (stores the memory address of) an object. c1 does point to an object. Java automatically runs the toString method on the object to which c1 points. The Coordinate2D toString method returns a String in the form (x, y).

The code segment prints:

(5, 6)

Memory diagram

c1 pointing to a box containing x with value 5 and y with value 6

The diagram is the same as in Step 3.

Additional classes & objects resources

Comments

Comment on Constructing objects