Creating an Instance of a Class

To use a class you have defined, you need to create an instance of that class. An instance is an object whose data type is the class. Any class you create can be instantiated, just like any other reference data type in Java.

The new Operator

Declaring a reference to an object in Java does not actually create an object.

The following declaration creates a reference to a Circle but does not create a Circle:

Circle myCircle;

Following this declaration, myCircle is a null reference if it's a field, or it is undefined if it's a method variable. Objects are created using the new operator as shown here:

Circle myCircle = new Circle();

The new operator is followed by the signature of the constructor to call for initialization ...

Get Special Edition Using Java 2 Standard Edition 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.