Java Reference
In-Depth Information
System
.
out
.
println
(
"Calling System.exit(), with f = "
+
f
);
System
.
exit
(
0
);
}
}
The bottom line? There's no guarantee at all on finalizers, but shutdown hooks have pretty
good odds of being run, at least in a standalone application (this will not work in a web ap-
plication, for example). But if you want to be sure, just write a method of your own and call
it!
Using Inner Classes
Problem
You need to write a private class, or a class to be used in one other class at most.
Solution
Use a nonpublic class or an inner class.
Discussion
A nonpublic class can be written as part of another class's source file, but not inside that
class. An inner class is Java terminology for a class defined inside another class. Inner
classes were first popularized with early Java for use as event handlers for GUI applications
(see
Action Handling: Making Buttons Work
)
, but they have a much wider application.
Inner classes can, in fact, be constructed in several contexts. An inner class defined as a
member of a class can be instantiated anywhere in that class. An inner class defined inside a
method can be referred to later only in the same method. Inner classes can also be named or
anonymous. A named inner class has a full name that is compiler-dependent; the standard
JVM uses a name like
MainClass$InnerClass
for the resulting file. An anonymous inner
class, similarly, has a compiler-dependent name; the JVM uses
MainClass$1
,
MainClass$2
,
and so on.
These classes cannot be instantiated in any other context; any explicit attempt to refer to, say,
OtherMainClass$InnerClass
, is caught at compile time:
oo/AllClasses.java