startup code

B

bob smith

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)
 
L

Lew

bob smith wrote, quoted or indirectly quoted someone who said :

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.
 
R

Robert Klemme

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
 
L

Lew

Robert said:
Lew wrote:
...

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.
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 
L

Lew

Arne said:
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.
 
R

Roedy Green

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?
 
B

bob smith

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.
 
B

bob smith

On 8/29/2012 8:14 AM, bob smith wrote:

...




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
 
E

Eric Sosman

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.
 
L

Lew

Eric said:
"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.
 
R

Robert Klemme

"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
 
E

Eric Sosman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top