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


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

Re: startup code

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!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, 29 Aug 2012 04:42:16 -0500
Date Wed, 29 Aug 2012 02:42:17 -0700
From Patricia Shanahan <pats@acm.org>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0
MIME-Version 1.0
Newsgroups comp.lang.java.programmer
Subject Re: startup code
References <9b4cce4e-99b1-414f-a033-1acf9200e90a@googlegroups.com> <2r4q38p3790lc86fpttn4arc58m5m1tjen@4ax.com> <3926176c-0940-4080-95a1-fb67bbbec50e@googlegroups.com> <llgr381to8gnakl65nvv8o5rjp7tesl1ai@4ax.com>
In-Reply-To <llgr381to8gnakl65nvv8o5rjp7tesl1ai@4ax.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Message-ID <GuKdnX_5kLJkfqDNnZ2dnUVZ_sKdnZ2d@earthlink.com> (permalink)
Lines 61
X-Usenet-Provider http://www.giganews.com
NNTP-Posting-Host 70.230.200.172
X-Trace sv3-3mdNrqM+mw6D1HnT66ZbjjRAJmj4E5R/Qu0l0VedcSXzcnh/WDwOkj26oP4uVTQ/zj+wlCpdPFANlIl!2pKgrPPGW76Itjv7f8uYhDK8nfBWP7TkHIHVBxaSVqmt87Yz5ddINBzfgMKKTpwxqnpFsptd1sfA!c6gAWk9S4xaEG2NizMUEk8OU9PJzzyYtNUHAjU+LXTnhQqg=
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 3296
Xref csiph.com comp.lang.java.programmer:18387

Show key headers only | View raw


On 8/29/2012 12:21 AM, Roedy Green wrote:
> On Tue, 28 Aug 2012 14:00:47 -0700 (PDT), Lew <lewbloch@gmail.com>
> wrote, quoted or indirectly quoted someone who said :
>
>>
>> The class init block won't run until the class is initialized
>
> I think he means by "the program" the class name invoked on the
> command line.  In that case the static init blocks in that class will
> execute even before the main method.

It is very important to say that only the class containing the main
method is initialized on program start-up. I would agree with "static
init block in the class containing the main method" as an answer to the
original question.

I did not see anything in the original question that identifies the
class whose code is to be run with the main method class. Even if that
is the OP's intent, we need to make sure that others will not be
confused by the answer. As a new Java programmer, I wasted some time
trying to get something in each of a set of classes to run on program
start-up.


>
> Under what conditions does a class load without running the static
> init blocks?
>

Any time it is loaded without being initialized. See 
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.1 
for the conditions that cause initialization.

For example, the following program:

public class ClassLoadLink {
   public static void main(String[] args) throws ClassNotFoundException {
     Class<?> c = Class.forName("TestClass", false,
         ClassLoadLink.class.getClassLoader());
     System.out.println("Class " + c.getName() + " loaded");
     new TestClass();
   }

}

class TestClass {
   static {
     System.out.println("In TestClass initialization");
   }
}

prints:

Class TestClass loaded
In TestClass initialization

The class TestClass has to be loaded prior to the first output line in
order to have a Class object. It does not get initialized until creation
of an instance.

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


Thread

startup code bob smith <bob@coolfone.comze.com> - 2012-08-28 11:48 -0700
  Re: startup code Roedy Green <see_website@mindprod.com.invalid> - 2012-08-28 11:53 -0700
    Re: startup code Lew <lewbloch@gmail.com> - 2012-08-28 14:00 -0700
      Re: startup code Robert Klemme <shortcutter@googlemail.com> - 2012-08-28 23:48 +0200
        Re: startup code Lew <lewbloch@gmail.com> - 2012-08-28 15:25 -0700
        Re: startup code Patricia Shanahan <pats@acm.org> - 2012-08-29 08:27 -0700
          Re: startup code bob smith <bob@coolfone.comze.com> - 2012-08-29 11:18 -0700
            Re: startup code Patricia Shanahan <pats@acm.org> - 2012-08-29 11:58 -0700
              Re: startup code Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-29 15:41 -0400
                Re: startup code Lew <lewbloch@gmail.com> - 2012-08-29 13:04 -0700
                Re: startup code Robert Klemme <shortcutter@googlemail.com> - 2012-08-29 22:06 +0200
                Re: startup code Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-29 17:14 -0400
      Re: startup code Roedy Green <see_website@mindprod.com.invalid> - 2012-08-29 00:21 -0700
        Re: startup code Patricia Shanahan <pats@acm.org> - 2012-08-29 02:42 -0700
          Re: startup code Patricia Shanahan <pats@acm.org> - 2012-08-29 02:54 -0700
    Re: startup code Arne Vajhøj <arne@vajhoej.dk> - 2012-08-28 19:20 -0400
  Re: startup code Jeff Higgins <jeff@invalid.invalid> - 2012-08-28 15:22 -0400
  Re: startup code Arne Vajhøj <arne@vajhoej.dk> - 2012-08-28 19:19 -0400

csiph-web