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)
(i.e. without explicitly calling into it)
Is there any way to add code to a class that will get executed whenever the program starts up?
<http://www.nofluffjuststuff.com/blo...her__a_pattern_to_bootstrap_java_applications>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)
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.
Robert said:Lew wrote:
...
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?
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)
You can put in it the main method or in a static init block.
Arne said: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.
The class init block won't run until the class is initialized
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?
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
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.
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.
"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.
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.