[chuck-users] initialization of static variables

Hans Aberg haberg at math.su.se
Wed Dec 9 13:01:00 EST 2009


On 9 Dec 2009, at 17:20, Robert Poor wrote:

> 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.

That is a good suggestion. Thanks.

> 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.

I discovered it the same way. Because of the current being error  
prone, I hope 'chuck' is changed so that class "static" data is just  
name-spaced otherwise global.

   Hans

  


More information about the chuck-users mailing list