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