Hans: Thanks for the note -- you are correct that we discussed the fact that static objects may not be initialized until the class is instantiated. 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. The distinction is simply whether you place your top-level initializer inside or outside of the class definition. So this form: ===== public class Nutz { static Object @ CONST; } new Object @=> Nutz.CONST; ===== ...initializes Nutz.CONST exactly once -- even BEFORE an instance of Nutz is created. But this form: ===== public class Soup { static Object @ CONST; new Object @=> Soup.CONST; } ===== ...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. 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. That's all. - Rob On 9 Dec 2009, at 04:20, Hans Aberg wrote:
On 9 Dec 2009, at 02:11, Robert Poor wrote:
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. It may avoid some astonishment.
The rules are simple, but are different than I thought:
-- top level forms INSIDE a class definition are executed every time an instance of that class is created.
-- top level forms OUTSIDE of a class definition are executed once when the file loads.
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.
Also, it seems possible to use "static" outside a class, but I do not know the semantics.
Hans