<div dir="ltr">Hi Gonzalo,<div><br></div><div>This is either a misfeature, bug, or exciting new programming paradigm, depending on your point of view :)</div><div><br></div><div>To explain:</div><div><span style="font-family:arial,sans-serif;font-size:13px">public class Foo {</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">  1 =&gt; static int bar;</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">}</span><br style="font-family:arial,sans-serif;font-size:13px"></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">static int bar; properly declares bar as a static variable. ChucK&#39;s &quot;pre-constructor&quot; concept executes all of the actual code in the body of the class every time the class is instantiated. For some reason this includes the 1 =&gt; static int bar; which just reduces to 1 =&gt; bar; since bar already exists. </span><br></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">Truthfully this is a bug that should be changed to only set the value once for static vars. In practice I do this:</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><div><span style="font-family:arial,sans-serif;font-size:13px">public class Foo {</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">  static int bar;</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">}</span><br style="font-family:arial,sans-serif;font-size:13px"></div></div><div><span style="font-family:arial,sans-serif;font-size:13px">1 =&gt; Foo.bar;</span></div><div><br></div><div>which achieves the desired outcome with only minor inconvenience. </div><div><br></div><div><span style="font-family:arial,sans-serif;font-size:13px">spencer</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 22, 2014 at 7:56 PM, Gonzalo <span dir="ltr">&lt;<a href="mailto:gonzalo@dense13.com" target="_blank">gonzalo@dense13.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
This is weird behavior I think:<br>
<br>
--------------------<br>
public class Foo {<br>
  1 =&gt; static int bar;<br>
}<br>
<br>
&lt;&lt;&lt; Foo.bar &gt;&gt;&gt;; // prints 0! Why?<br>
<br>
Foo f;<br>
&lt;&lt;&lt; Foo.bar &gt;&gt;&gt;;  // prints 1, ok<br>
<br>
2 =&gt; Foo.bar;<br>
&lt;&lt;&lt; Foo.bar &gt;&gt;&gt;;  // prints 2, ok<br>
<br>
Foo f2;<br>
&lt;&lt;&lt; Foo.bar &gt;&gt;&gt;; // prints 1 again!<br>
--------------------<br>
<br>
So it seems that static members get initialized only on instantiation, but to make it funky, on every instantiation...<br>
<br>
Is this a buggy implementation of static members, or am I missing something? Not complaining, just a question. :)<br>
<br>
Thanks!<span class="HOEnZb"><font color="#888888"><br>
Gonzalo<br>
______________________________<u></u>_________________<br>
chuck-users mailing list<br>
<a href="mailto:chuck-users@lists.cs.princeton.edu" target="_blank">chuck-users@lists.cs.<u></u>princeton.edu</a><br>
<a href="https://lists.cs.princeton.edu/mailman/listinfo/chuck-users" target="_blank">https://lists.cs.princeton.<u></u>edu/mailman/listinfo/chuck-<u></u>users</a><br>
<br>
<br>
</font></span></blockquote></div><br></div>