Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Joshua Cranmer Newsgroups: comp.lang.java.programmer Subject: Re: trigger static init Date: Wed, 11 Apr 2012 09:39:15 -0500 Organization: A noiseless patient Spider Lines: 27 Message-ID: References: <5605o7di8le7conanmf0g70p1ltipeqct4@4ax.com> <17423607.56.1333992983521.JavaMail.geo-discussion-forums@pbkr6> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 11 Apr 2012 14:39:19 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="WpcHJSul77m+zlbR9GVqkA"; logging-data="21805"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX184kpNV516s7P6sWZsKe/+icpSUF9h9A6Q=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 In-Reply-To: Cancel-Lock: sha1:9pWUi2LXhW4yA/DhuMeXOZ/WnL8= Xref: csiph.com comp.lang.java.programmer:13476 On 4/11/2012 7:52 AM, Andreas Leitgeb wrote: > Lew wrote: >> Referencing the 'class' literal does not incur class initialization. > > from JLS 12.4.1: > A class [...] T is initialized immediately before first [...] > T is a class and an instance of T is created. > T is a class and a static method declared by T is invoked. > A static field declared by T is assigned. > A static field declared by T is used [ ... and not constant ... ] > T is a top-level class, and an assert statement (ยง14.10) lexically > nested within T is executed. > > Does using a class-literal in "synchronized(T.class) {...}" count > in for implicitly using the monitor-field of the Class-instance, or is > it strictly undefined (or defined somewhere else)? So, the use of "T.class" creates an object of Class, which is not sufficient to cause T to be initialized. Some more inspection of the JLS (and some experiments) leads me to the conclusion that a synchronized(T.class) block can race with the initialization of T. Experiments confirm that synchronized(T.class) does not cause initialization of T. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth