Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #13483
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail |
|---|---|
| NNTP-Posting-Date | Wed, 11 Apr 2012 11:32:38 -0500 |
| Date | Wed, 11 Apr 2012 09:32:31 -0700 |
| From | Patricia Shanahan <pats@acm.org> |
| User-Agent | Mozilla/5.0 (Windows NT 5.2; WOW64; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 |
| MIME-Version | 1.0 |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: trigger static init |
| 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> <k7SdnU0xr8MbEhjSnZ2dnUVZ_j2dnZ2d@earthlink.com> <slrnjob8s8.kvi.avl@gamma.logic.tuwien.ac.at> |
| In-Reply-To | <slrnjob8s8.kvi.avl@gamma.logic.tuwien.ac.at> |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| Message-ID | <77CdnRAKBte7LxjSnZ2dnUVZ_tGdnZ2d@earthlink.com> (permalink) |
| Lines | 95 |
| X-Usenet-Provider | http://www.giganews.com |
| NNTP-Posting-Host | 70.230.201.141 |
| X-Trace | sv3-Ld9miDImmfbpXUGplu2cU9HmwxaYHVqjtTG8rPkTeQ63jiMvJ8+pMj5dwZTLERHpI+kl95YwdFHDDr3!VI6rIRTs7ObL2gPOx7GYxD62aWCRygCMXgT5O+jzWVMhlljwkzCpY1mAtfSq5uGIfgUzk/DgpQEe!+OXV/qWtzdZq8po1JXCNsxAKnWJOEmSFL5de3watLa8WiaA= |
| X-Abuse-and-DMCA-Info | Please be sure to forward a copy of ALL headers |
| X-Abuse-and-DMCA-Info | Otherwise we will be unable to process your complaint properly |
| X-Postfilter | 1.3.40 |
| X-Original-Bytes | 4462 |
| Xref | csiph.com comp.lang.java.programmer:13483 |
Show key headers only | View raw
On 4/11/2012 8:30 AM, Andreas Leitgeb wrote:
> Patricia Shanahan<pats@acm.org> wrote:
>> On 4/11/2012 5:52 AM, Andreas Leitgeb wrote:
>>> T is a top-level class, and an assert statement (§14.10) lexically
>>> nested within T is executed.
>>> Btw., I don't understand the point about assert. If the assert is
>>> lexically nested in T and gets executed, then I'd have naively thought,
>>> that one of the first two points would necessarily have happened,
>>> anyway, before the assert could be reached. What am I missing here?
>> Suppose S is a static class defined in T. I interpreted that as meaning
>> that executing an assert in S would trigger initialization of T.
>> Or suppose S is non-static, but the assert is in its static code.
>
> I don't yet get it.
>
> === Main.java ===
> public class Main {
> public static void main(String[] args) {
> Object foo = new Outer.Inner();
> }
> }
> class Outer {
> static {
> System.out.println("Outer init");
> }
> public static class Inner {
> static {
> System.out.println("Inner init1");
> assert 1==1 : "oups!";
> System.out.println("Inner init2");
> }
> }
> }
> === call ===
> $ java -ea Other ;# ditto without the -ea
> Inner init1
> Inner init2
> $ java -version
> java version "1.6.0_26"
> Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
> Java HotSpot(TM) Server VM (build 20.1-b02, mixed mode)
> $
> === end of SSCCE ===
>
> So, it *is* possible to run code "lexically included in Outer"
> without Outer having been initialized first (which I had wrongly
> believed to be impossible), but assert itself doesn't seem to
> have the specified effect. I also read §14.10, but that just
> affirms, that (in order to determine the Assertion-flag for the
> toplevel class "Outer") "Outer" would be initialized, too.
> It doesn't seem to happen, though.
>
I may be right about what the JLS comment meant, but it is clear the
implementation is not following it. I enabled assertions, and added a
non-constant assertion that would fail. It still does not initialize
the outer class:
=== Main.java ===
public class Main {
public static void main(String[] args) {
Object foo = new Outer.Inner();
Outer.Inner.innerMethod(5);
}
}
class Outer {
static {
System.out.println("Outer init");
}
public static class Inner {
static {
System.out.println("Inner init1");
assert 1 == 1 : "oups!";
System.out.println("Inner init2");
}
static void innerMethod(int arg1) {
System.out.println("innerMethod 1");
assert arg1 == 1 : "oops!";
System.out.println("innerMethod 2");
}
}
}
=== call ===
Inner init1
Inner init2
Exception in thread "main" java.lang.AssertionError: oops!
innerMethod 1
at Outer$Inner.innerMethod(Main.java:22)
at Main.main(Main.java:4)
=== end of SSCCE ===
Patricia
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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