Import does not correlate with any instructions in compiled code. They set aliases for use only at compile time.
There are some reflective methods that allow you to load a class but not yet initialized, but in most cases you can assume that whenever a class is referenced, it has been initialized.
Static member initializers and static blocks are executed as if they were a single static initializer block in source code order.
The object referenced by the static member variable is strictly referenced until the class is unloaded. A regular ClassLoader never unloads a class, but those used by application servers run under the right conditions. However, this is a complex area and is the source of many hard-to-diagnose memory leaks, but another reason not to use global variables.
As a (tangential) bonus, here's a tricky question:
public class Foo { private static Foo instance = new Foo(); private static final int DELTA = 6; private static int BASE = 7; private int x; private Foo() { x = BASE + DELTA; } public static void main(String... argv) { System.out.println(Foo.instance.x); } }
What will this code print? Try it and you will see that it prints "6". There are several things here, and one of them is the order of static initialization. The code runs as if it were written as follows:
public class Foo { private static Foo instance; private static final int DELTA = 6; private static int BASE; static { instance = null; BASE = 0; instance = new Foo(); BASE = 7; } private int x; private Foo() { x = BASE + 6; } }
erickson Jan 01 '09 at 20:04 2009-01-01 20:04
source share