Java Reference
In-Depth Information
Body . The code must increment nextID so that the next Body object cre-
ated 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 or-
bits 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 re-
ferring 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.
 
Search WWH ::




Custom Search