Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!news.albasani.net!.POSTED!not-for-mail From: j4n bur53 Newsgroups: comp.lang.java.programmer Subject: Re: Are Outer Classes Loaded Lazily? Date: Thu, 21 Sep 2017 01:18:12 +0200 Organization: albasani.net Lines: 55 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net Pi/Ul4J2kVDXiHNr2ijkA/veFGjGbNjTRK9nx/zJj7Wd8rFJdQ3Tl9f7sG8MJSIe0brnsiB/4C5gBARLSh/wXg== NNTP-Posting-Date: Wed, 20 Sep 2017 23:18:12 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="+11Ohw/EaGTEkiu5hDKuGh0CozO7p1J/8udn8lg1USZY7nMBeu45QzqoBPZSvBRVoBLEIhPWWyZNOqfTnPbPPV+HjyIam/omYjR3Jivp50Nh57AzuW8OkhshK3DpJwUa"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0 SeaMonkey/2.48 In-Reply-To: Cancel-Lock: sha1:BmGVIT+zOlV6tyQkFwdE9a6LiIc= Xref: csiph.com comp.lang.java.programmer:37439 for nested classes the method getEnclosingClass(), calls getDeclaringClass(), which is native. but I guess it will use the outer_class_info_index in the class file format. which is a little unnecessary, since it could derive the outer class name from the inner class name, lastIndexOf('$') or somesuch. but your testing somehow indicates that getDeclaringClass() indeed works lazily. Can this be? This would imply that it can be possible that the static of Inner might be called before the static of Outer? Or is this an artefact of reflection. Since in real code we habe a name Outer.Inner and since this mentions Outer, it would load Outer? Eric Sosman schrieb: > On 9/20/2017 1:23 PM, j4n bur53 wrote: >> Assume I have a nested inner class Inner, >> and I do the following: >> >> Class x=Class.forName('../Outer$Inner'); > > One or two tiny little problems here ... ;) > >> And then I do the following: >> >> Class y=x.getEnclosingClass(); >> >> Is the enclosing class '../Outer' already >> loaded during the Class.forName() or only >> when I do getEnclosingClass() ? > > You'd need to scrutinize the JLS and/or the JVMS to be > certain. That can be difficult: They seek exactness and > sometimes find it on the other side of opacity ... > > Instead, I ran a quick experiment: I wrote suitable > Outer and Inner classes, plus a Foo class to make the > forName() and getEnclosingClass() calls sandwiched by > some System.out.println() calls, and ran it as > > java -verbose:class esosman.misc.Foo > > This told me that forName() caused Inner to be loaded > but not Outer, and that getEnclosingClass() caused > Outer to be loaded but not initialized. (I also found > that Inner was initialized during forName() if it was > made `static', but couldn't tell what happened when it > was non-`static'.) >