THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

2.4. Creating Objects


In this first version of Body, objects that represent particular celestial bodies are created and initialized like
this:


Body sun = new Body();
sun.idNum = Body.nextID++;
sun.name = "Sol";
sun.orbits = null; // in solar system, sun is middle


Body earth = new Body();
earth.idNum = Body.nextID++;
earth.name = "Earth";
earth.orbits = sun;


First we declare a reference variable (sun) that can refer to objects of type Body. As mentioned before, such
a declaration does not create an object; it only defines a variable that references objects. The object it refers to
must be created explicitly.


We create the object sun refers to using new. The new construct is by far the most common way to create
objects (we cover the other ways in Chapter 16). When you create an object with new, you specify the type of
object you want to create and any arguments for its construction. The runtime system allocates enough space
to store the fields of the object and initializes it in ways you will soon see. When initialization is complete, the
runtime system returns a reference to the new object.


If the system cannot find enough free space to create the object, it may have to run the garbage collector to try
to reclaim space. If the system still cannot find enough free space, new throws an OutOfMemoryError
exception.


Having created a new Body object, we initialize its variables. Each Body object needs a unique identifier,
which it gets from the static nextID field of Body. The code must increment nextID so that the next
Body object created will get a unique identifier.


We then make an earth object in a similar fashion. This example builds a solar system model. In this model,
the sun is in the center, and sun's orbits field is null because it doesn't orbit anything. When we create
and initialize earth, we set its orbits field to sun. A moon object would have its orbits field set to
earth. In a model of the galaxy, the sun would orbit around the black hole presumed to be at the middle of
the Milky Way.


You create objects using new, but you never delete them explicitly. The virtual machine manages memory for
you using garbage collection, which means that objects that you cannot possibly use any longer can have their
space automatically reclaimed by the virtual machine without your intervention. If you no longer need an
object you should cease referring to it. With local variables in methods this can be as simple as returning from
the methodwhen the method is no longer executing none of its variables can possibly be used. More durable
references, such as fields of objects, can be set to null. Memory management is discussed in more detail in
Chapter 17.


Exercise 2.5: Write a main method for your Vehicle class that creates a few vehicles and prints their field
values.


Exercise 2.6: Write a main method for your LinkedList class that creates a few objects of type Vehicle
and places them into successive nodes in the list.

Free download pdf