Method should call once and only once in its lifecycle
Solution 1
Static initializer blocks are executed only once when the classloader loads the class. The time they're executed is so bound to your application logic. To be more precise different classloaders might load your class so the static block can be executed more than one time theoretically.
For scheduling purposes try using out of the box a scheduler library, for example Quartz scheduler. ( http://quartz-scheduler.org) This might seem a bit of overhead the first time, however these libs offer advanced features that you may need eventually. Just a simple example: what if your program is stopped and restarted in an hour? Then the process might be run twice in this particular hour. Using quartz you can handle this situation as well.
Solution 2
You need a variable, too.
class ...
private static hasRun = false;
public static synchronize boolean runOnce ()
{
if (hasRun) return false;
hasRun = true;
// do something
return true;
}
A static block may never be called when there is no usage of this class.
There is a discussion about unloading classes Unloading classes in java?
When unloading classes happens than multiple loading are possible than multiple call of static initializer may happen. However, in this case my solution will fail as in this case ANY solution must fail.
I feel this is extremely uncommon and unlikely. But maybe you have to case when you not control the environment
NIVESH SENGAR
I m a funluving person working as a Software developer. This is my learning phase and hope I'll learn sumthing from u guys...:)
Updated on June 04, 2022Comments
-
NIVESH SENGAR almost 2 years
I have a method which is actually a scheduler which runs a process in every one hour and create a log file every hour.
I want to call this method once in the application life cycle so I call it from a static block.
But I feel this is not working because the file is sometimes generated in one hour and sometimes early. I heard somewhere that static block only execute once is it not true?
If yes than what should I do? -
jabal about 12 yearsstatic block is not a static method
-
jabal about 12 yearsstatic block is not a static method
-
Churk about 12 yearsI do believe and I quote
I have a method
, is the first thing the OP states here. -
nwinkler about 12 yearsI agree with your solution, but two comments: a) Your first paragraph seems wrong, the question is referring to a static code block in a class, not a static method. b) What is a static class?
-
stefan bachert about 12 yearsthat is right, but a static block is executed when the class becomes in use. The question does not sound to request this
-
jabal about 12 yearshe says "so I put it in static block."
-
stefan bachert about 12 yearsThat was what he has done, not necessarily what he wants to achieve
-
stefan bachert about 12 yearsBy the way, the correct term by Oracle/sun is: Static Initialization Blocks. So "static block" could also interpreted as set of static "members"
-
NIVESH SENGAR about 12 yearsThanks jabal but I am using quartz schedulr and facing this problem after that:)
-
NIVESH SENGAR about 12 yearsI have a method is right but I call this method from static block dude
-
jabal about 12 yearsMaybe you should pose a different question about the quartz issue then. Probably you just did not use is correctly as it is a very high quality product used by millions. I'm quite sure the community could help you with that.