Java Reference
In-Depth Information
represent the origin might be common enough that you should provide
it as a static field in the
Point
class:
public static Point origin = new Point();
If this declaration appears inside the declaration of the
Point
class, there
will be exactly one piece of data called
Point.origin
that always refers
to an object at (0.0, 0.0). This static field is there no matter how many
Point
objects are created, even if none are created. The values of
x
and
y
are zero because that is the default for numeric fields that are not ex-
plicitly initialized to a different value.
You can probably see now why named constants are declared
static
.
ject field, although the term
non-static field
is sometimes used for clar-
ity.
1.7.3. The Garbage Collector
After creating an object with
new
, how do you get rid of the object when
you no longer want it? The answer is simplestop referring to it. Unrefer-
enced objects are automatically reclaimed by a
garbage collector,
which
runs in the background and tracks object references. When an object
is no longer referenced, the garbage collector can remove it from the
storage allocation heap, although it may defer actually doing so until a
propitious time.