Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > startup code

Reply
Thread Tools

startup code

 
 
bob smith
Guest
Posts: n/a
 
      08-29-2012
On Tuesday, August 28, 2012 4:48:52 PM UTC-5, Robert Klemme wrote:
> 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?



I have a font class, and it needs to load a bitmap containing the fonts.






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

 
Reply With Quote
 
 
 
 
bob smith
Guest
Posts: n/a
 
      08-29-2012
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-29-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-29-2012
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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-29-2012
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/
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-29-2012
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
(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dlink DWL-G650 (WinXP) works, but not at boot/startup (Code 10) Ohaya Wireless Networking 4 08-27-2008 02:31 AM
accessing a registered startup script in c# code Andy B ASP .Net 6 01-02-2008 10:01 PM
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM
problems with startup code in static libraries. Bramz C++ 3 01-29-2004 01:34 PM



Advertisments