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


Groups > comp.lang.java.programmer > #18356 > unrolled thread

startup code

Started bybob smith <bob@coolfone.comze.com>
First post2012-08-28 11:48 -0700
Last post2012-08-28 19:19 -0400
Articles 18 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#18356 — startup code

Frombob smith <bob@coolfone.comze.com>
Date2012-08-28 11:48 -0700
Subjectstartup code
Message-ID<9b4cce4e-99b1-414f-a033-1acf9200e90a@googlegroups.com>
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)

[toc] | [next] | [standalone]


#18357

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-08-28 11:53 -0700
Message-ID<2r4q38p3790lc86fpttn4arc58m5m1tjen@4ax.com>
In reply to#18356
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 

[toc] | [prev] | [next] | [standalone]


#18362

FromLew <lewbloch@gmail.com>
Date2012-08-28 14:00 -0700
Message-ID<3926176c-0940-4080-95a1-fb67bbbec50e@googlegroups.com>
In reply to#18357
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

[toc] | [prev] | [next] | [standalone]


#18365

FromRobert Klemme <shortcutter@googlemail.com>
Date2012-08-28 23:48 +0200
Message-ID<aa4sm3FtuvU1@mid.individual.net>
In reply to#18362
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/

[toc] | [prev] | [next] | [standalone]


#18367

FromLew <lewbloch@gmail.com>
Date2012-08-28 15:25 -0700
Message-ID<e1808943-1586-45e0-85e4-97ae9df6f738@googlegroups.com>
In reply to#18365
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

[toc] | [prev] | [next] | [standalone]


#18396

FromPatricia Shanahan <pats@acm.org>
Date2012-08-29 08:27 -0700
Message-ID<DdmdnV3jtJyVqKPNnZ2dnUVZ_jCdnZ2d@earthlink.com>
In reply to#18365
On 8/29/2012 8:14 AM, bob smith wrote:
...
> I have a font class, and it needs to load a bitmap containing the fonts.

When do you need the bitmap? For example, it might be needed on first
call to some static method in the class, or the first time an instance
of the class is created ...

Patricia

[toc] | [prev] | [next] | [standalone]


#18403

Frombob smith <bob@coolfone.comze.com>
Date2012-08-29 11:18 -0700
Message-ID<2667fee7-d27c-4d64-8fcc-501c188a2d33@googlegroups.com>
In reply to#18396
On Wednesday, August 29, 2012 10:28:09 AM UTC-5, Patricia Shanahan wrote:
> On 8/29/2012 8:14 AM, bob smith wrote:
> 
> ...
> 
> > I have a font class, and it needs to load a bitmap containing the fonts.
> 
> 
> 
> When do you need the bitmap? For example, it might be needed on first
> 
> call to some static method in the class, or the first time an instance
> 
> of the class is created ...
> 
> 
> 
> Patricia

On first call to some static method in the class

[toc] | [prev] | [next] | [standalone]


#18409

FromPatricia Shanahan <pats@acm.org>
Date2012-08-29 11:58 -0700
Message-ID<wN6dnbtMWafH-6PNnZ2dnUVZ_rOdnZ2d@earthlink.com>
In reply to#18403
On 8/29/2012 11:18 AM, bob smith wrote:
> On Wednesday, August 29, 2012 10:28:09 AM UTC-5, Patricia Shanahan wrote:
>> On 8/29/2012 8:14 AM, bob smith wrote:
>>
>> ...
>>
>>> I have a font class, and it needs to load a bitmap containing the fonts.
>>
>>
>>
>> When do you need the bitmap? For example, it might be needed on first
>>
>> call to some static method in the class, or the first time an instance
>>
>> of the class is created ...
>>
>>
>>
>> Patricia
>
> On first call to some static method in the class
>

In that case, put the initialization in a static initializer:

static {
   // create the bitmap
}

It will be run on the first event that causes initialization of the
class. Invocation of a static method is one of those events.

Patricia

[toc] | [prev] | [next] | [standalone]


#18410

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-08-29 15:41 -0400
Message-ID<k1lr9i$t23$1@dont-email.me>
In reply to#18409
On 8/29/2012 2:58 PM, Patricia Shanahan wrote:
> On 8/29/2012 11:18 AM, bob smith wrote:
>> On Wednesday, August 29, 2012 10:28:09 AM UTC-5, Patricia Shanahan wrote:
>>> On 8/29/2012 8:14 AM, bob smith wrote:
>>>
>>> ...
>>>
>>>> I have a font class, and it needs to load a bitmap containing the
>>>> fonts.
>>>
>>>
>>>
>>> When do you need the bitmap? For example, it might be needed on first
>>>
>>> call to some static method in the class, or the first time an instance
>>>
>>> of the class is created ...
>>>
>>>
>>>
>>> Patricia
>>
>> On first call to some static method in the class
>>
>
> In that case, put the initialization in a static initializer:
>
> static {
>    // create the bitmap
> }
>
> It will be run on the first event that causes initialization of the
> class. Invocation of a static method is one of those events.

     "What she said," with a stylistic suggestion: If the code to
create the bitmap grows to more than a very few lines, consider
putting them in a private static method of their own and calling
that method from the static initializer:

	class Thing {
	    ...
	    static {
	        createTheBitmap();
	    }

	    /** Called only during class initialization. */
	    private static void createTheBitmap() {
	        // create the bitmap
	    }
	    ...
	}

Doesn't change the code's meaning in any significant way, but
may make it easier to debug/adapt/refactor later on.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#18412

FromLew <lewbloch@gmail.com>
Date2012-08-29 13:04 -0700
Message-ID<ac25f74d-ae9a-45cb-9aa7-2ae3077fe870@googlegroups.com>
In reply to#18410
Eric Sosman wrote:
> Patricia Shanahan wrote:
>>  bob smith wrote:
>>> Patricia Shanahan wrote:
>>>> bob smith wrote: (
>>>> ...
>>>>> I have a font class, and it needs to load a bitmap containing the
>>>>> fonts.
> 
>>>> When do you need the bitmap? For example, it might be needed on first
>>>> call to some static method in the class, or the first time an instance
>>>> of the class is created ...
> 
>>> On first call to some static method in the class
> 
>> In that case, put the initialization in a static initializer:
> 
>> static {
>>    // create the bitmap
>> }
> 
>> It will be run on the first event that causes initialization of the
>> class. Invocation of a static method is one of those events.
> 
>      "What she said," with a stylistic suggestion: If the code to
> create the bitmap grows to more than a very few lines, consider
> putting them in a private static method of their own and calling
> that method from the static initializer:
> 
> 	class Thing {
> 	    ...
> 	    static {
> 	        createTheBitmap();
> 	    }
> 
> 	    /** Called only during class initialization. */
> 	    private static void createTheBitmap() {
> 	        // create the bitmap
> 	    }
> 
> 	    ...
> 
> 	}
> 
> Doesn't change the code's meaning in any significant way, but
> may make it easier to debug/adapt/refactor later on.

Note to the OP - 

Several terms have been tossed around. You may notice ones that 
stand out as terms of art - "class intialization", "static initializer", 
"(class) loading", "private static method" and so on. 

It's worthwhile to search on "Java term" for each term you find here 
that isn't 100% clear to you already (where "term" is the term 
that you wish to research), e.g., "Java class initialization".

It doesn't do a body a whole lot of good to understand that their 
method will run at class initialization if they don't know what class 
initialization is.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#18413

FromRobert Klemme <shortcutter@googlemail.com>
Date2012-08-29 22:06 +0200
Message-ID<aa7b30Fa7aU1@mid.individual.net>
In reply to#18410
On 29.08.2012 21:41, Eric Sosman wrote:
>      "What she said," with a stylistic suggestion: If the code to
> create the bitmap grows to more than a very few lines, consider
> putting them in a private static method of their own and calling
> that method from the static initializer:
>
>      class Thing {
>          ...
>          static {
>              createTheBitmap();
>          }
>
>          /** Called only during class initialization. */
>          private static void createTheBitmap() {
>              // create the bitmap
>          }
>          ...
>      }
>
> Doesn't change the code's meaning in any significant way, but
> may make it easier to debug/adapt/refactor later on.

If you go through the effort why then call it from an initializer?  Why 
not just via the field declaration that holds the bitmap?

class Thing {
   private static final BitMap bm = loadTheBitmap();

   private static BitMap loadTheBitmap() {
     ...
     return ...;
   }
}

Assuming that the state will be held in this class.

Kind regards

	robert


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

[toc] | [prev] | [next] | [standalone]


#18417

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-08-29 17:14 -0400
Message-ID<k1m0mu$1bl$1@dont-email.me>
In reply to#18413
On 8/29/2012 4:06 PM, Robert Klemme wrote:
> On 29.08.2012 21:41, Eric Sosman wrote:
>>      "What she said," with a stylistic suggestion: If the code to
>> create the bitmap grows to more than a very few lines, consider
>> putting them in a private static method of their own and calling
>> that method from the static initializer:
>>
>>      class Thing {
>>          ...
>>          static {
>>              createTheBitmap();
>>          }
>>
>>          /** Called only during class initialization. */
>>          private static void createTheBitmap() {
>>              // create the bitmap
>>          }
>>          ...
>>      }
>>
>> Doesn't change the code's meaning in any significant way, but
>> may make it easier to debug/adapt/refactor later on.
>
> If you go through the effort why then call it from an initializer?  Why
> not just via the field declaration that holds the bitmap?
>
> class Thing {
>    private static final BitMap bm = loadTheBitmap();
>
>    private static BitMap loadTheBitmap() {
>      ...
>      return ...;
>    }
> }
>
> Assuming that the state will be held in this class.

     If the result of "create the bitmap" is a single value that
is stored in a single field, sure -- but that's an assumption.
For more general class initialization I think it would be
misleading to hide the setup of several fields behind what
looks like an initializer for just one of them.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#18382

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-08-29 00:21 -0700
Message-ID<llgr381to8gnakl65nvv8o5rjp7tesl1ai@4ax.com>
In reply to#18362
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 

[toc] | [prev] | [next] | [standalone]


#18387

FromPatricia Shanahan <pats@acm.org>
Date2012-08-29 02:42 -0700
Message-ID<GuKdnX_5kLJkfqDNnZ2dnUVZ_sKdnZ2d@earthlink.com>
In reply to#18382
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.

[toc] | [prev] | [next] | [standalone]


#18389

FromPatricia Shanahan <pats@acm.org>
Date2012-08-29 02:54 -0700
Message-ID<FaKdnTpEHO5Le6DNnZ2dnUVZ_rOdnZ2d@earthlink.com>
In reply to#18387
On 8/29/2012 2:42 AM, Patricia Shanahan wrote:
> 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.
...

Correction to my own post. In addition to the class that contains the
main method, there are other classes, such as its superclasses, classes
needed to execute its initialization code, and system classes that do
get initialized on start-up.

If a class is not needed to run the main method it does not even get
loaded, let alone initialized.

Patricia

[toc] | [prev] | [next] | [standalone]


#18369

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-08-28 19:20 -0400
Message-ID<503d522f$0$285$14726298@news.sunsite.dk>
In reply to#18357
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


[toc] | [prev] | [next] | [standalone]


#18358

FromJeff Higgins <jeff@invalid.invalid>
Date2012-08-28 15:22 -0400
Message-ID<k1j5os$jpd$1@dont-email.me>
In reply to#18356
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_applications>

[toc] | [prev] | [next] | [standalone]


#18368

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-08-28 19:19 -0400
Message-ID<503d51f2$0$285$14726298@news.sunsite.dk>
In reply to#18356
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web