Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   startup code (http://www.velocityreviews.com/forums/t951498-startup-code.html)

bob smith 08-28-2012 06:48 PM

startup code
 
Is there any way to add code to a class that will get executed whenever the program starts up?

(i.e. without explicitly calling into it)

Roedy Green 08-28-2012 06:53 PM

Re: startup code
 
On Tue, 28 Aug 2012 11:48:41 -0700 (PDT), bob smith
<bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone
who said :

>Is there any way to add code to a class that will get executed whenever the program starts up?


You can put in it the main method or in a static init block.

See http://mindprod.com/jgloss/static.html
--
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04



Jeff Higgins 08-28-2012 07:22 PM

Re: startup code
 
On 08/28/2012 02:48 PM, bob smith wrote:
> Is there any way to add code to a class that will get executed whenever the program starts up?
>
> (i.e. without explicitly calling into it)

<http://www.nofluffjuststuff.com/blog/vladimir_vivien/2011/10/loader_launcher__a_pattern_to_bootstrap_java_appli cations>


Lew 08-28-2012 09:00 PM

Re: startup code
 
On Tuesday, August 28, 2012 11:53:34 AM UTC-7, Roedy Green wrote:
> bob smith wrote, quoted or indirectly quoted someone who said :
>>Is there any way to add code to a class that will get executed whenever the program starts up?

>
> You can put in it the main method or in a static init block.
> See http://mindprod.com/jgloss/static.html


The main method technique will happen whenever the program starts up.

The class init block won't run until the class is initialized (not necessarily
when it's loaded). This can be quite a while after the program starts "up".
It can even be quite a while after the class is loaded.

--
Lew

Robert Klemme 08-28-2012 09:48 PM

Re: startup code
 
On 28.08.2012 23:00, Lew wrote:
> On Tuesday, August 28, 2012 11:53:34 AM UTC-7, Roedy Green wrote:
>> bob smith wrote, quoted or indirectly quoted someone who said :
>>> Is there any way to add code to a class that will get executed whenever the program starts up?

>>
>> You can put in it the main method or in a static init block.
>> See http://mindprod.com/jgloss/static.html

>
> The main method technique will happen whenever the program starts up.
>
> The class init block won't run until the class is initialized (not necessarily
> when it's loaded). This can be quite a while after the program starts "up".
> It can even be quite a while after the class is loaded.


Still it is often early enough, i.e. before instances of the very class
get to do their work.

Bob, what are you trying to accomplish? Or is this more like a homework
question?

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Lew 08-28-2012 10:25 PM

Re: startup code
 
Robert Klemme wrote:
> Lew wrote:
> ...
>> The class init block won't run until the class is initialized (not necessarily
>> when it's loaded). This can be quite a while after the program starts "up".
>> It can even be quite a while after the class is loaded.

>
> Still it is often early enough, i.e. before instances of the very class
> get to do their work.


"Early enough" depends on whether the OP wants what they literally asked for,
something that happens at program start, or the more relaxed requirement of
before the class is otherwise used.

> Bob, what are you trying to accomplish? Or is this more like a homework
> question?


Bob, this is a key question Robert asks.

--
Lew

Arne Vajhøj 08-28-2012 11:19 PM

Re: startup code
 
On 8/28/2012 2:48 PM, bob smith wrote:
> Is there any way to add code to a class that will get executed whenever the program starts up?
>
> (i.e. without explicitly calling into it)


Create a new class with a main that:
- does whatever you want done
- calls the original main
and run that.

Arne



Arne Vajhøj 08-28-2012 11:20 PM

Re: startup code
 
On 8/28/2012 2:53 PM, Roedy Green wrote:
> On Tue, 28 Aug 2012 11:48:41 -0700 (PDT), bob smith
> <bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone
> who said :
>> Is there any way to add code to a class that will get executed whenever the program starts up?

>
> You can put in it the main method or in a static init block.


That does not run code when the program startup.

It will run the code when the class is first loaded.

Which may not even happen.

Arne




Lew 08-28-2012 11:24 PM

Re: startup code
 
Arne Vajhøj wrote:
> Roedy Green wrote:
>> bob smith wrote, quoted or indirectly quoted someone who said :
>>> Is there any way to add code to a class that will get executed wheneverthe program starts up?

>
>> You can put in it the main method or in a static init block.

>
> That does not run code when the program startup.
> It will run the code when the class is first loaded.


No.

It will run the code when the class is initialized, which might
be considerably after the class is first loaded.

> Which may not even happen.


Same with initialization - a class might be loaded and never
initialized.

--
Lew

Roedy Green 08-29-2012 07:21 AM

Re: startup code
 
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.

Under what conditions does a class load without running the static
init blocks?
--
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04




All times are GMT. The time now is 05:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57