Path: csiph.com!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: trigger static init Date: Wed, 11 Apr 2012 08:08:36 -0700 Organization: albasani.net Lines: 41 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 X-Trace: news.albasani.net s5DGdVMasKJ8i5nem/Mix7psWbXF5DHvCvhvj7GoIdKVSqRuyaRVe7wSDMURaZDkzXECojUlm6XxnU+WUjd45JKMR6yhJpYGwFCzSxg0pE9vLvfVWvfC8xGCmsm117A2 NNTP-Posting-Date: Wed, 11 Apr 2012 15:08:38 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="mYUsqMz8pfI7/9G+2UUIfx9GnHL28JiTTGv5q3Pz2kx+bUPGeOJU9AvRcRcUhDsd0UMda3QO8vlBq5P7YgLDLMozpiZYikv3skr0y5nyN9g8fU0zh5XozicSiDXidk6H"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1 In-Reply-To: Cancel-Lock: sha1:XAVkIrV0yGZPCU3ezdT8yqYQVgc= Xref: csiph.com comp.lang.java.programmer:13478 On 04/11/2012 07:39 AM, Joshua Cranmer wrote: > 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. It used to, in contravention of the JLS. "A class or interface will not be initialized under any other circumstance." It's important to note that the Java Language Specification explicitly forbids class initialization from use of the 'class' literal. Prior to Java 5 there was a bug that it did, which Sun fixed with version 5. This caused certain libraries (notably Apache Commons) to break where they relied on the buggy behavior, when run under Java 5+ under certain corner scenarios. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg