Method should call once and only once in its lifecycle

10,326

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

Share:
10,326
NIVESH SENGAR
Author by

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, 2022

Comments

  • NIVESH SENGAR
    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
    jabal about 12 years
    static block is not a static method
  • jabal
    jabal about 12 years
    static block is not a static method
  • Churk
    Churk about 12 years
    I do believe and I quote I have a method, is the first thing the OP states here.
  • nwinkler
    nwinkler about 12 years
    I 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
    stefan bachert about 12 years
    that is right, but a static block is executed when the class becomes in use. The question does not sound to request this
  • jabal
    jabal about 12 years
    he says "so I put it in static block."
  • stefan bachert
    stefan bachert about 12 years
    That was what he has done, not necessarily what he wants to achieve
  • stefan bachert
    stefan bachert about 12 years
    By 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
    NIVESH SENGAR about 12 years
    Thanks jabal but I am using quartz schedulr and facing this problem after that:)
  • NIVESH SENGAR
    NIVESH SENGAR about 12 years
    I have a method is right but I call this method from static block dude
  • jabal
    jabal about 12 years
    Maybe 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.