Object creation

Objects are declared and created. When we write an object declaration, or declare an object, we are declaring a name or identifier so that we can refer to an object. For example, the following line of code declares myObjectName as the name of an object of type CapuchinMonkey. At this point, no object was created and no memory allocated for it:

    CapuchinMonkey myObjectName;

We use the new keyword to create an object. The following example illustrates how to invoke the new operation to create an object. This operation results in:

    myObjectName = new CapuchinMonkey();

Of course, we can combine the declaration and creation statements together by using CapuchinMonkey myObjectName = new CapuchinMonkey(); instead of CapuchinMonkey ...

Get Java 9: Building Robust Modular Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.