Java Reference
In-Depth Information
by other programs. The JVM performs automatic
garbage collection
to reclaim the
mem-
ory
occupied by objects that are no longer used. When there are
no more references
to an
object, the object is
eligible
to be collected. Collection typically occurs when the JVM ex-
ecutes its
garbage collector
, which may not happen for a while, or even at all before a pro-
gram terminates. So, memory leaks that are common in other languages like C and C++
(because memory is
not
automatically reclaimed in those languages) are
less
likely in Java,
but some can still happen in subtle ways. Resource leaks other than memory leaks can also
occur. For example, an app may open a file on disk to modify its contents—if the app does
not close the file, it must terminate before any other app can use the file.
A Note about Class
Object
's
finalize
Method
Every class in Java has the methods of class
Object
(package
java.lang
), one of which is
method
finalize
. (You'll learn more about class
Object
in Chapter 9.) You should
never
use method
finalize
, because it can cause many problems and there's uncertainty as to
whether it will
ever
get called before a program terminates.
The original intent of
finalize
was to allow the garbage collector to perform
termi-
nation housekeeping
on an object just before reclaiming the object's memory. Now, it's
considered better practice for any class that uses system resources—such as files on disk—
to provide a method that programmers can call to release resources when they're no longer
needed in a program.
AutoClosable
objects reduce the likelihood of resource leaks when
you use them with the
try
-with-resources statement. As its name implies, an
AutoClos-
able
object is closed automatically, once a
try
-with-resources statement finishes using the
object. We discuss this in more detail in Section 11.12.
Software Engineering Observation 8.8
Many Java API classes (e.g., class
Scanner
and classes that read files from or write files to
disk) provide
close
or
dispose
methods that programmers can call to release resources
when they're no longer needed in a program.
Every object has its own copy of all the instance variables of the class. In certain cases, only
one copy of a particular variable should be
shared
by all objects of a class. A
static
field
—
called a
class variable
—is used in such cases. A
static
variable represents
classwide infor-
mation
—all objects of the class share the
same
piece of data. The declaration of a
static
variable begins with the keyword
static
.
Motivating
static
Let's motivate
static
data with an example. Suppose that we have a video game with
Mar-
tian
s and other space creatures. Each
Martian
tends to be brave and willing to attack other
space creatures when the
Martian
is aware that at least four other
Martian
s are present. If
fewer than five
Martian
s are present, each of them becomes cowardly. Thus, each
Martian
needs to know the
martianCount
. We could endow class
Martian
with
martianCount
as
an
instance variable
. If we do this, then every
Martian
will have
a separate copy
of the in-
stance variable, and every time we create a new
Martian
, we'll have to update the instance
variable
martianCount
in every
Martian
object. This wastes space with the redundant cop-
ies, wastes time in updating the separate copies and is error prone. Instead, we declare
mar-