Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: startup code Date: Wed, 29 Aug 2012 15:41:36 -0400 Organization: A noiseless patient Spider Lines: 58 Message-ID: References: <9b4cce4e-99b1-414f-a033-1acf9200e90a@googlegroups.com> <2r4q38p3790lc86fpttn4arc58m5m1tjen@4ax.com> <3926176c-0940-4080-95a1-fb67bbbec50e@googlegroups.com> <2667fee7-d27c-4d64-8fcc-501c188a2d33@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 29 Aug 2012 19:41:38 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="ffb8f7085759b339c1002252b48331a4"; logging-data="29763"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+k4Tc33TI5cq534+69O9X" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: Cancel-Lock: sha1:WhHTXlfC4L0AxgWSlNib0m0r5vo= Xref: csiph.com comp.lang.java.programmer:18410 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 esosman@ieee-dot-org.invalid