<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hans:<div><br></div><div>Thanks for the note -- you are correct that we discussed the fact that static objects may not be initialized until the class is instantiated. &nbsp;</div><div><br></div><div>You CAN "throw some data into the class for use with static functions," and have that data initialized exactly once before an instance is created. &nbsp;The distinction is simply whether you place your top-level initializer inside or outside of the class definition. &nbsp;So this form:</div><div>=====</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public class Nutz {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&nbsp;&nbsp;static Object @ CONST;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>new Object @=&gt; Nutz.CONST;</div><div><div>=====</div><div>...initializes Nutz.CONST exactly once -- even BEFORE an instance of Nutz is created. &nbsp;But this form:</div><div>=====</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public class Soup {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>&nbsp;&nbsp;static Object @ CONST;&nbsp;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Object @=&gt; Soup.CONST;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><div>=====</div><div>...does NOT initialize Soup.CONST until an instance of Soup is created, and worse, it sets Soup.CONST to a new value every time a new instance is created.</div><div><br></div><div>As I said, this is not a profound revelation, but it was the source of a devilish bug in my code and I thought I could spare someone the headache by pointing it out. &nbsp;That's all.</div><div><br></div><div>- Rob</div></div></div><div><br></div><div><div><div>On 9 Dec 2009, at 04:20, Hans Aberg wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>On 9 Dec 2009, at 02:11, Robert Poor wrote:<br><br><blockquote type="cite">I now understand a bit more about initialization of static variables -- more accurately, about execution of top-level code -- and while it's not earth-shattering, I thought I'd pass it along. &nbsp;It may avoid some astonishment.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">The rules are simple, but are different than I thought:<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">-- top level forms INSIDE a class definition are executed every time an instance of that class is created.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">-- top level forms OUTSIDE of a class definition are executed once when the file loads.<br></blockquote><br>Yes, that is what was discussed in the "static bug" thread. If one throws in some "static" data into the class for use with static functions, that data is not initialized until there has been an object of that class initialized. And the static data is probably re-initialized every time an new object is initialized.<br><br>Also, it seems possible to use "static" outside a class, but I do not know the semantics.<br><br> &nbsp;Hans<font class="Apple-style-span" color="#000000"><font class="Apple-style-span" color="#144FAE"><br></font></font></div></blockquote></div></div></body></html>