I thought this was interesting:
As long as you make sure that a Java class is available before calling its
methods or constructor(s), it only needs to be known to the Java compiler,
i. e. only at compile time -- and not necessarily at runtime as well. Also,
import statements using the name of the class and even declarations using
the (imported or fully qualified) class name are always fine -- even if the
class is not actually available at runtime.
I wrote some short example code to illustrate this. In main/, there
is the main class ImportTest. It is not part of any (explicit) package.
That class imports test.Importee and declares a private static member
variable of that type. It only instantiates an instance of test.Importee
and calls one of its methods if the first command line parameter is true.
- To compile, execute
javac */*.javafrom the top-level directory. - Now change into
main/and dojava ImportTest. This should work and printI can run! Whee!. Note thattest.Importeeis not present in the classpath. - Now, from the same directory, do
java ImportTest true. This will fail sincetest.Importeeis not present in the classpath... - That is fixed by
java -cp ..:. ImportTest true: Now the JVM can accesstest.Importeeand the program will run, successfully executing the calls totest.Importee. Neat!