Next:
Constructors
Up:
Classes and objects
Previous:
Access Control and Inheritance
Contents
Creating Objects
Objects are created using the
new
construct:
Body sun = new Body(); Body earth; earth = new Body();
This example declare two references (sun, earth) that can refer to objects of type Body.
Using
new
is the most common way to create objects.
Java runtime (a) allocates enough space to store the fields of the object, (b) initializes the object, and (c) returns a reference to the new object.
If the system can't find enough free space, it runs the garbage collector.
If there is not enough free space available, new will throw an OutOfMemoryError exception.
Next:
Constructors
Up:
Classes and objects
Previous:
Access Control and Inheritance
Contents