<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/05/more-on-static-code.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.23738-</id>
  <updated>2009-11-16T15:43:54Z</updated>
  <title>Comments for More on Static Code (http://www.insideria.com/2008/05/more-on-static-code.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23738</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/05/more-on-static-code.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=23738" title="More on Static Code" />
    <published>2008-05-16T07:06:00Z</published>
    <updated>2008-05-16T12:46:29Z</updated>
    <title>More on Static Code</title>
    <summary>One of the most popular past posts on my blog adamflater.net has been Static Code Blocks!. Due to the amount of requests for that post I thought this would be a good topic to feature on InsideRIA regarding ActionScript and static code.</summary>
    <author>
      <name>Adam Flater</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<div class="ap_r" style="margin: 16px;"><a href="http://www.insideria.com/upload/2008/05/more_on_static_code/static.jpg" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/upload/2008/05/more_on_static_code/static.jpg" alt="static.jpg" title="Click to enlarge" width="148"/></a></div>

<p>One of the most popular posts on my blog <a href="http://adamflater.net">adamflater.net</a> has been <a href="http://adamflater.blogspot.com/2007/03/static-code-blocks.html">Static Code Blocks!</a>. Due to the amount of requests for that post I thought it made for a good topic to feature on InsideRIA regarding ActionScript and static code.</p>

<p>First off, what is static code? Static code is executable code that is said to belong to a Class. An instance method of an Object exists N times for the N instances of the Class type of Object, but static code exists only once no matter how many instances of the Object exist. The other difference between an instance method and static code is that static code has a static scope. In other words, the code executing in the static block may only access the other static members of the Class that it belongs to.</p>

<p>There are essentially three ways to create static code blocks in ActionScript: Using the Mixin Class meta tag, wrapping the code in an unnamed block, and using a static method initializer pattern. In reviewing each of these solutions it's important to consider order of execution and scope.</p>

<p>So, let's take a look at these three solutions in the order that they're executed.</p>

<p></p>

<p><b>1. Using a static var as an initializer</b></p>

<p>This solution is executed by the Flash Player first out of the other two solutions. It involves using a static variable to call a static method.</p>

<p>Generally it looks like this: </p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre> 
<span class="category1">private</span> <span class="category1">static</span> <span class="category1">var</span> initialized : <span class="category2">Boolean</span> = initializer();
<span class="category1">private</span> <span class="category1">static</span> <span class="category1">function</span> initializer() : <span class="category2">Boolean</span>
{
 	<span class="category2">trace</span>( "<span class="quote">static method init'er</span>" );
 	<span class="category1">return</span> <span class="category1">true</span>;
} </pre>
</code>
 
</div></div> 

<p>In this example <em>initialized</em> is a static variable that technically belongs to the Class (not the Object). Although this variable is accessible by the Object it "lives" at the Class level. When the class is being loaded by the Flash Player the variable <em>initialized</em> is created and assigned the value returned by the <em>initializer</em> method. Like the other examples, this code is executed once no matter how many times the Class it belongs to is instantiated. </p>

<p><b>2. Unnamed code block</b></p>

<p>If, for some reason, you're using the static initializer pattern shown in the first example and you also have an unnamed code block in your class, the unnamed code block will be executed second. A simpler example is:</p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre> 
{
 	<span class="category2">trace</span>( "<span class="quote">this is a static block</span>" );
}</pre>
</code>
 
</div></div> 

<p>Again, the scope of this block is the static scope (ie Class members are accessible). </p>


<p><b>3. The Mixin meta tag</b></p>

<p>The last to execute in this series of static code blocks is the Mixin meta tag. An example of this solution is:</p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;"> 
<code language="perl">
<pre> 
[Mixin]
<span class="category1">public</span> <span class="category1">class</span> StaticBlocks
{
 	<span class="category1">public</span> <span class="category1">static</span> <span class="category1">function</span> <span class="category2">init</span>( systemManager : ISystemManager ) : <span class="category1">void</span> 
 	{
  		<span class="category2">trace</span>( "<span class="quote">mix-in init method</span>" );
  	}	
 	...</pre>
</code>
 
</div></div> 

<p>In this solution the <em>init</em> method also has a static scope limited to the Class it belongs to, but in addition there is a reference passed into the method to the application's SystemManager called <em>systemManager</em>. The system manager provides access to many interesting areas of your application. SystemManager is also available via the static reference: <em>Application.application.systemManager</em> however, in the previous two examples the classes needed to obtain a reference the SystemManager using <em>Application.application.systemManager</em> have not yet been created. By using the Mixin tag you're guaranteed that the Flash Player / Flex Framework has initialized and created the Application and SystemManger classes. </p>

<p>It's equally important to note that the other two solutions executed before the Application and SystemManager have finished initializing. This is important if you have some code that you need to have executed before these classes finish their instantiations. </p>

<p>Aside from initializing custom styles in components you may not find a lot of uses for static code blocks in a typical Flex application, but these techniques can come in handy if you're building a service API or Framework in Flex.</p>

<p>I hope this was a valuable follow up post to my original effort outline static code blocks. I'd love to hear your questions and comments.</p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23738-comment:2017211</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23738" type="text/html" href="http://www.insideria.com/2008/05/more-on-static-code.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/05/more-on-static-code.html#comment-2017211" />
    <title>Comment from Anthony McCormick on 2008-05-18</title>
    <author>
        <name>Anthony McCormick</name>
        <uri>http://www.betadesigns.co.uk/Blog</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.betadesigns.co.uk/Blog">
        <![CDATA[<p>Hi Adam,<br />
I often use your first method described for creating default styles for my components, these styles then can be overwritten by someone using the component however there will always be my initial stying applied if they arent overwritten.</p>

<p>private static var STYLES : Boolean = defaultStyles( );<br />
		<br />
		private static function defaultStyles( ) : Boolean<br />
		{<br />
			//NO CSS DECLARATIONS<br />
			var newStyle : CSSStyleDeclaration = new CSSStyleDeclaration( );<br />
			if( !StyleManager.getStyleDeclaration( 'yOffSet' ) )<br />
			{<br />
					newStyle.setStyle( 'yOffSet', 30 );<br />
			}StyleManager.setStyleDeclaration( 'CustomComponentName', newStyle, true );<br />
			<br />
			return true;<br />
		}</p>]]>
    </content>
    <published>2008-05-18T12:28:53Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23738-comment:2017248</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23738" type="text/html" href="http://www.insideria.com/2008/05/more-on-static-code.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/05/more-on-static-code.html#comment-2017248" />
    <title>Comment from Adam Flater on 2008-05-19</title>
    <author>
        <name>Adam Flater</name>
        <uri>http://adamflater.net</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://adamflater.net">
        <![CDATA[<p>@anthony</p>

<p>Yeah.. I should have mentioned that this is the method described in LiveDocs to do just that. Thanks for adding this to the post.</p>]]>
    </content>
    <published>2008-05-20T03:44:49Z</published>
  </entry>

</feed
