Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #13478

Re: trigger static init

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: trigger static init
Date 2012-04-11 08:08 -0700
Organization albasani.net
Message-ID <jm46pm$8uk$1@news.albasani.net> (permalink)
References <5605o7di8le7conanmf0g70p1ltipeqct4@4ax.com> <m2zkalqs1m.fsf@ipa.eternal-september.org> <17423607.56.1333992983521.JavaMail.geo-discussion-forums@pbkr6> <slrnjoavkt.kvi.avl@gamma.logic.tuwien.ac.at> <jm452n$l9d$1@dont-email.me>

Show all headers | View raw


On 04/11/2012 07:39 AM, Joshua Cranmer wrote:
> On 4/11/2012 7:52 AM, Andreas Leitgeb wrote:
>> Lew<lewbloch@gmail.com> 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<T.class> in "synchronized(T.class) {...}" count
>> in for implicitly using the monitor-field of the Class<T>-instance, or is
>> it strictly undefined (or defined somewhere else)?
>
> So, the use of "T.class" creates an object of Class<T>, 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."
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.1>

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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

trigger static init Roedy Green <see_website@mindprod.com.invalid> - 2012-04-08 23:28 -0700
  Re: trigger static init Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-04-09 10:32 +0300
    Re: trigger static init Roedy Green <see_website@mindprod.com.invalid> - 2012-04-09 06:09 -0700
      Re: trigger static init markspace <-@.> - 2012-04-09 09:19 -0700
      Re: trigger static init Lew <lewbloch@gmail.com> - 2012-04-09 10:33 -0700
    Re: trigger static init Lew <lewbloch@gmail.com> - 2012-04-09 10:36 -0700
      Re: trigger static init Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-04-11 12:52 +0000
        Re: trigger static init Patricia Shanahan <pats@acm.org> - 2012-04-11 07:04 -0700
          Re: trigger static init Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-04-11 15:30 +0000
            Re: trigger static init Patricia Shanahan <pats@acm.org> - 2012-04-11 09:32 -0700
              Re: trigger static init Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-04-22 16:09 +0000
          Re: trigger static init Steven Simpson <ss@domain.invalid> - 2012-04-12 07:38 +0100
            Re: trigger static init Patricia Shanahan <pats@acm.org> - 2012-04-12 00:36 -0700
        Re: trigger static init Joshua Cranmer <Pidgeot18@verizon.invalid> - 2012-04-11 09:39 -0500
          Re: trigger static init Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2012-04-11 14:56 +0000
          Re: trigger static init Lew <noone@lewscanon.com> - 2012-04-11 08:08 -0700
  Re: trigger static init glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-04-09 09:18 +0000
    Re: trigger static init Roedy Green <see_website@mindprod.com.invalid> - 2012-04-09 04:57 -0700
    Re: trigger static init Lew <lewbloch@gmail.com> - 2012-04-09 10:31 -0700
  Re: trigger static init Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-04-10 12:22 -0700

csiph-web