startup code

Discussion in 'Java' started by bob smith, Aug 28, 2012.

  1. bob smith

    bob smith Guest

    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, Aug 28, 2012
    #1
    1. Advertising

  2. bob smith

    Roedy Green Guest

    On Tue, 28 Aug 2012 11:48:41 -0700 (PDT), 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
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    A new scientific truth does not triumph by convincing its opponents and making them see the light,
    but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
    ~ Max Planck 1858-04-23 1947-10-04
    Roedy Green, Aug 28, 2012
    #2
    1. Advertising

  3. bob smith

    Jeff Higgins Guest

    On 08/28/2012 02:48 PM, bob smith wrote:
    > 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)

    <http://www.nofluffjuststuff.com/blog/vladimir_vivien/2011/10/loader_launcher__a_pattern_to_bootstrap_java_applications>
    Jeff Higgins, Aug 28, 2012
    #3
  4. bob smith

    Lew Guest

    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.

    --
    Lew
    Lew, Aug 28, 2012
    #4
  5. 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?

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Aug 28, 2012
    #5
  6. bob smith

    Lew Guest

    Robert Klemme wrote:
    > Lew wrote:
    > ...
    >> 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.


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

    --
    Lew
    Lew, Aug 28, 2012
    #6
  7. bob smith

    Arne Vajhøj Guest

    On 8/28/2012 2:48 PM, bob smith wrote:
    > 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
    Arne Vajhøj, Aug 29, 2012
    #7
  8. bob smith

    Arne Vajhøj Guest

    On 8/28/2012 2:53 PM, Roedy Green wrote:
    > On Tue, 28 Aug 2012 11:48:41 -0700 (PDT), 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.


    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
    Arne Vajhøj, Aug 29, 2012
    #8
  9. bob smith

    Lew Guest

    Arne Vajhøj wrote:
    > 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 wheneverthe program starts up?

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


    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.

    --
    Lew
    Lew, Aug 29, 2012
    #9
  10. bob smith

    Roedy Green Guest

    On Tue, 28 Aug 2012 14:00:47 -0700 (PDT), Lew <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >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?
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    A new scientific truth does not triumph by convincing its opponents and making them see the light,
    but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
    ~ Max Planck 1858-04-23 1947-10-04
    Roedy Green, Aug 29, 2012
    #10
  11. bob smith

    bob smith Guest

    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/
    bob smith, Aug 29, 2012
    #11
  12. bob smith

    bob smith Guest

    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
    bob smith, Aug 29, 2012
    #12
  13. bob smith

    Eric Sosman Guest

    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
    d
    Eric Sosman, Aug 29, 2012
    #13
  14. bob smith

    Lew Guest

    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
    Lew, Aug 29, 2012
    #14
  15. 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/
    Robert Klemme, Aug 29, 2012
    #15
  16. bob smith

    Eric Sosman Guest

    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
    d
    Eric Sosman, Aug 29, 2012
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wally

    Startup key

    Wally, Oct 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    657
    Jeff Johnson [MVP: VB]
    Oct 19, 2004
  2. Martin Altemark
    Replies:
    1
    Views:
    302
    Natty Gur
    Aug 21, 2003
  3. VB Programmer

    Startup Kit

    VB Programmer, Mar 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    510
    Curt_C [MVP]
    Mar 1, 2004
  4. Bramz
    Replies:
    3
    Views:
    382
    Bramz
    Jan 29, 2004
  5. Andy B
    Replies:
    6
    Views:
    421
    marss
    Jan 2, 2008
Loading...

Share This Page