Java Reference
In-Depth Information
continued
you have no way to refer to this object anymore (which is now floating around
nameless somewhere in the computer memory), the JVM will automatically
clean up this piece of memory (this is performed by a mechanism called gar-
bage collection) to remove (destroy) the object. The same happens when
assigning
null
to the variable with the second object. The variable now refers
to nothing, and the second object can also no longer be referenced, and is
thus removed as well.
Whenever the JVM cleans up objects in this manner, it will call a special built‐in
method,
finalize()
, on them. Programmers can implement their own final-
ization method if they desire, which comes in handy when objects are utiliz-
ing resources that need to be cleanly closed when they are removed (think,
for example, about open network connections). In practice, however, there is
rarely a need to override the default behavior.
Again, you need to learn how to walk before you can run, so for now, just be
glad about the fact that Java takes care of these cleanup aspects for you. You
do not need to concern yourself with writing destructors.
the main method
Earlier in this chapter, you learned that there exists one special method used as an entry point to
actually execute (run) your program—the so‐called
main
method.
The
main
method can be defined in any class, but is
always
defined as:
public static void main(String[] args) {
}
Meaning that the
main
method:
➤
Is publicly accessible. (
public
is an access modifier. I have ignored access modifiers for now,
but for the
main
method, you
have
to define it.)
➤
Is a
static
class method, as no objects exist yet when a program is started.
➤
Returns nothing (
void
).
➤
Takes one argument, the arguments passed to the program.
Let's have a closer look at the
main
method. Suppose you once more define a simple
Book
class to
look as follows:
class Book {
final String title;
final int releaseYear;
int copiesSold;
Search WWH ::
Custom Search