Static initializer and the Singleton pattern

Bryce McKinlay bryce@waitaki.otago.ac.nz
Thu Nov 22 18:50:00 GMT 2001


Weiqi Gao wrote:

>The following bit of code was distilled from a bigger application that
>exhibited a case of interference between the singleton pattern and
>static initializers.  Contrary to the naive expectation, under Sun's JDK
>this program prints 'false'.  And a check against the JVM spec confirms
>that the behavior is the expected bahavior.
>
>class Main {
>    public static void main(String[] args) {
>        System.out.println(A.getInstance() == B.a);
>    }
>}
>

Hmm... the problem is that GCJ is calling the static initializer for B 
from Main.main() before it calls A.getInstance().

regards

Bryce.




More information about the Java mailing list