12.4.1
When Initialization Occurs
EXECUTION
Before a class is initialized, its superclasses are initialized, if they have not
previously been initialized.
Thus, the test program:
class Super {
static { System.out.print("Super "); }
}
class One {
static { System.out.print("One "); }
}
class Two extends Super {
static { System.out.print("Two "); }
}
class Test {
public static void main(String[] args) {
One o = null;
Two t = new Two();
System.out.println((Object)o == (Object)t);
}
}
prints:
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 initialized.
A reference to a field is an active use of only the class or interface that actu
ally declares it, even though it might be referred to through the name of a subclass,
a subinterface, or a class that implements an interface. The test program:
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);
}
}
prints only:
1729
224
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved