-
Bug
-
Resolution: Not an Issue
-
P3
-
None
-
1.1.4
-
generic
-
generic
allan.jacobs@Eng 1997-10-15
Superclasses are supposed to be initialized before their subclasses
are initialized. If a static initializer in a superclass invokes
a static method declared in it's subclass then this ordering of
initializations is disrupted.
Test.java compiles and executes correctly.
Bad.java does not execute correctly. The static initializers for Y and
Z are executed in the wrong order. The problem is caused by the presence
of the Test.func() invokation in a static initializer in class Z.
algol% cat Test.java
class Z {
static { System.out.println("Z "); }
static int i;
int x = 8;
float f = 0;
static boolean flag;
static {
// i = Test.func();
i = 1;
flag = true;
}
static { System.out.println("Z: Z.i="+Z.i); }
static { System.out.println("Z: Z.flag="+Z.flag); }
}
class Y extends Z {
static { System.out.println("Y "); }
static {
i = 12;
flag = false;
}
static { System.out.println("Y: Z.i="+Z.i); }
static { System.out.println("Y: Z.flag="+Z.flag); }
}
class Test extends Y {
static { System.out.println("Test "); }
static int func() {
return 1;
}
public static void main(String argv[]) {
System.out.println("i="+i+". Should be 12.");
System.out.println("flag="+flag+". Should be false.");
}
}
algol% javac -d . Test.java
algol% java Test
Z
Z: Z.i=1
Z: Z.flag=true
Y
Y: Z.i=12
Y: Z.flag=false
Test
i=12. Should be 12.
flag=false. Should be false.
algol% cat Bad.java
class Z {
static { System.out.println("Z "); }
static int i;
int x = 8;
float f = 0;
static boolean flag;
static {
i = Bad.func();
// i = 1;
flag = true;
}
static { System.out.println("Z: Z.i="+Z.i); }
static { System.out.println("Z: Z.flag="+Z.flag); }
}
class Y extends Z {
static { System.out.println("Y "); }
static {
i = 12;
flag = false;
}
static { System.out.println("Y: Z.i="+Z.i); }
static { System.out.println("Y: Z.flag="+Z.flag); }
}
class Bad extends Y {
static { System.out.println("Bad "); }
static int func() {
return 1;
}
public static void main(String argv[]) {
System.out.println("i="+i+". Should be 12.");
System.out.println("flag="+flag+". Should be false.");
}
}
algol% javac -d . Bad.java
algol% java Bad
Z
Y
Y: Z.i=12
Y: Z.flag=false
Bad
Z: Z.i=1
Z: Z.flag=true
i=1. Should be 12.
flag=true. Should be false.