The New Operator Tutorial

The new Java keyword and operator is essential to creating objects and calling a class constructor. There are basically three steps to create an object.

  • Declaration · Declare a reference variable of object data type.
  • Instantiation · The new operator allocates the object in memory and returns a reference to that object.
  • Initialization · The new operator then invokes the appropriate constructor based on the argument list.
Basically, the structure for creating an object is this:
(object type) (reference variable) (assignment operator =) (new operator) (constructor(arguments)) (semicolon;)
This tutorial will discuss in detail all three steps to create an object.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md NewOperator
C:\Java>cd NewOperator
C:\Java\NewOperator>Notepad NewOperator.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class NewOperator {
    public static void main(String args[]) {
        Acme a; // declare a reference variable of object type Acme
        a = null; // assign the reference variable a null value;
        System.out.println("a = " + a);

        System.out.println("----------");
        // the new operator Instantiates an Acme object in memory returns a reference to the new object. 
        // the assignment operator assigns the returned reference to the reference variable.
        a = new Acme(); 
        System.out.println(a);
                                 
        System.out.println("----------");
        // loop 5 times showing what the NEW operator returns as a reference 
        for (int i = 0; i < 5; i++) {
            System.out.println(new Acme()); // display the return value of the new operator when it Instantiates an Acme object.
        }
        // Note: the output will differ with regards to the memory address, but all references will begin with Acme@
    }
}

class Acme {
    // empty class
}


Now switch back to the command prompt (CMD) and type in javac NewOperator.java and press Enter.
Now type in java NewOperator and press Enter.


C:\Java\NewOperator>javac NewOperator.java
C:\Java\NewOperator>java NewOperator
a = null
----------
Acme@659e0bfd
----------
Acme@2a139a55
Acme@15db9742
Acme@6d06d69c
Acme@7852e922
Acme@4e25154f

Now the last thing to demonstrate is the initialization step where the new operator then invokes the appropriate constructor based on the argument list. I will code a constructor to the Acme class that simply displays a string literal to the console.
Overwrite the following code into Notepad and be sure to save the file when you are done.


class NewOperator {
    public static void main(String args[]) {
        Acme a = new Acme(); 
        System.out.println(a);
    }
}
class Acme {
    Acme(){ // constructor
        System.out.println("The Acme constructor just executed all statements in its code block!");
    }
}

Now switch back to the command prompt (CMD) and type in javac NewOperator.java and press Enter.
Now type in java NewOperator and press Enter.


C:\Java\NewOperator>javac NewOperator.java
C:\Java\NewOperator>java NewOperator
The Acme constructor just executed all statements in its code block!
Acme@659e0bfd


Final thoughts

Understanding what the new operator does is the key to understanding how objects are created. In addition to this tutorial, my Reference Variables Tutorial and my Constructors Tutorial will help solidify your understanding of creating objects.


Tutorials