Java Reference
In-Depth Information
Super Two false
The class
One
is never initialized, because it not used actively and therefore is never
linked to. The class
Two
is initialized only after its superclass
Super
has been initial-
ized.
Example 12.4.1-2. Only The Class That Declares
static
Field Is Initialized
class Super {
static int taxi = 1729;
}
class Sub extends Super {
static { System.out.print("Sub "); }
}
class Test {
public static void main(String[] args) {
System.out.println(Sub.taxi);
}
}
This program prints only:
1729
because the class
Sub
is never initialized; the reference to
Sub.taxi
is a reference to a
field actually declared in class
Super
and does not trigger initialization of the class
Sub
.
Example 12.4.1-3. Interface Initialization Does Not Initialize Superinterfaces
interface I {
int i = 1, ii = Test.out("ii", 2);
}
interface J extends I {
int j = Test.out("j", 3), jj = Test.out("jj", 4);
}
interface K extends J {
int k = Test.out("k", 5);
}
class Test {
public static void main(String[] args) {
System.out.println(J.i);
System.out.println(K.j);
}
static int out(String s, int i) {