On 3/10/10 17:22, Ary Borenszweig wrote:
Jacob Carlborg wrote:
On 3/10/10 01:15, Walter Bright wrote:
Ellery Newcomer wrote:
I hate the restriction on modules with static constructors and cyclic
dependencies. IMO it's the most patronizing 'feature' D has.
I understand, but the alternative is the essentially random order of
execution that C++ has.
How does Java handle this ?
Static initialization blocks are associated to classes. They are run
when the class is loaded. So it depends on the order classes are loaded
in your program (it's the order in which you instantiate them).
---
public class A {
static {
System.out.println("A loading");
B b = new B();
System.out.println("A loaded");
}
}
public class B {
static {
System.out.println("B loading");
A a = new A();
System.out.println("B loaded");
}
}
public class C {
public static void main(String[] args) {
A a = new A();
// Prints:
// A loading
// B loading
// B loaded
// A loaded
}
}
public class D {
public static void main(String[] args) {
B b = new B();
// Prints:
// B loading
// A loading
// A loaded
// B loaded
}
}
I forgot for a second that Java forces you to put everything in classes.