Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18412

Re: startup code

Newsgroups comp.lang.java.programmer
Date 2012-08-29 13:04 -0700
References (4 earlier) <c9e3596e-4091-4661-943a-a62f7e1ac4e0@googlegroups.com> <DdmdnV3jtJyVqKPNnZ2dnUVZ_jCdnZ2d@earthlink.com> <2667fee7-d27c-4d64-8fcc-501c188a2d33@googlegroups.com> <wN6dnbtMWafH-6PNnZ2dnUVZ_rOdnZ2d@earthlink.com> <k1lr9i$t23$1@dont-email.me>
Message-ID <ac25f74d-ae9a-45cb-9aa7-2ae3077fe870@googlegroups.com> (permalink)
Subject Re: startup code
From Lew <lewbloch@gmail.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

startup code bob smith <bob@coolfone.comze.com> - 2012-08-28 11:48 -0700
  Re: startup code Roedy Green <see_website@mindprod.com.invalid> - 2012-08-28 11:53 -0700
    Re: startup code Lew <lewbloch@gmail.com> - 2012-08-28 14:00 -0700
      Re: startup code Robert Klemme <shortcutter@googlemail.com> - 2012-08-28 23:48 +0200
        Re: startup code Lew <lewbloch@gmail.com> - 2012-08-28 15:25 -0700
        Re: startup code Patricia Shanahan <pats@acm.org> - 2012-08-29 08:27 -0700
          Re: startup code bob smith <bob@coolfone.comze.com> - 2012-08-29 11:18 -0700
            Re: startup code Patricia Shanahan <pats@acm.org> - 2012-08-29 11:58 -0700
              Re: startup code Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-29 15:41 -0400
                Re: startup code Lew <lewbloch@gmail.com> - 2012-08-29 13:04 -0700
                Re: startup code Robert Klemme <shortcutter@googlemail.com> - 2012-08-29 22:06 +0200
                Re: startup code Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-29 17:14 -0400
      Re: startup code Roedy Green <see_website@mindprod.com.invalid> - 2012-08-29 00:21 -0700
        Re: startup code Patricia Shanahan <pats@acm.org> - 2012-08-29 02:42 -0700
          Re: startup code Patricia Shanahan <pats@acm.org> - 2012-08-29 02:54 -0700
    Re: startup code Arne Vajhøj <arne@vajhoej.dk> - 2012-08-28 19:20 -0400
  Re: startup code Jeff Higgins <jeff@invalid.invalid> - 2012-08-28 15:22 -0400
  Re: startup code Arne Vajhøj <arne@vajhoej.dk> - 2012-08-28 19:19 -0400

csiph-web