This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
Re: Static initializer and the Singleton pattern
- From: Bryce McKinlay <bryce at waitaki dot otago dot ac dot nz>
- To: Weiqi Gao <weiqigao at networkusa dot net>
- Cc: java at gcc dot gnu dot org
- Date: Fri, 23 Nov 2001 15:51:37 +1300
- Subject: Re: Static initializer and the Singleton pattern
- References: <1006478866.1855.0.camel@gao-2001.stlouis.mo.us>
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.