Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A 'constant' (<code>const</code>) is an object that can only be set once and must be set in close proximity (generally immediately) to the declaration of the object. So you can't do this:</p> <pre><code>const val:String; ... 5 minutes later ... val = 'hello world' </code></pre> <p>Instead, you must do:</p> <pre><code>const val:String = 'hello world'; </code></pre> <p>From the docs, it appears you can set it later than this, but there does not seem to be a predefined time or order in which you can set it. You are best off setting it immediately as I did above. If you compile in "strict mode", it <strong>must</strong> be set as I did above.</p> <p>A constant is good for process time. Because it is a fixed value, the runtime can store it differently and in a much more efficient manner. I cannot remember specifics, but consts offer substantial improvements in object-access-time over a standard variable.</p> <p><code>static</code> is an access modifier, just as <code>private</code> or <code>public</code> are (there quite a few others and I suggest you read up on all of them). Static objects exist only once. A standard object is declared every single time its class is instantiated/accessed. A static object is only declared the very first time. So consider the following</p> <pre><code>public class ClassA { public static var URL:String = "http://google.com"; public function ClassA(){ //hello world } } </code></pre> <p>That is a simple class with a <code>public static var</code> stored in it. When I instantiate the class as so:</p> <pre><code>var hello:ClassA = new ClassA(); </code></pre> <p><code>URL</code> is created and set to Google's URL. Now, a static property does not belong to an object, so if you attempt to access <code>hello.URL</code>, it will fail. Instead, it belongs to the class definition. So you access it as <code>ClassA.URL</code>. Within ClassA's scope, you can access it via <code>ClassA.URL</code> or just through <code>URL</code>. Generally, static objects are used in two places:</p> <ol> <li>Util classes. <code>Math.PI</code> or <code>Math.max()</code> are good examples of this</li> <li>Objects where you need to save a single instance. I like to use them for User systems where you have the ability to sign in as a single user only at any given time. I generally have a <code>User</code> object where I store <code>username</code> and <code>accountType</code> and the like. </li> </ol> <p>A static object is both good and bad. For a Class that will be instantiated a thousand times and the value will never change, it offers a substantial memory usage benefit (instead of storing 15 bytes x 1000, or 1.5 kilobytes, you only store the first 15 bytes). However, there are scope issues and general OOP standards to consider. You will find people who will argue until they are blue in the face that static objects, and by association Singletons (read up on those, they are not the same as a static object), should never be used because it goes against OOP principles. My example of using it for a User object is debatable if it should be static or not. Some would argue it should be a Singleton (my latest project used a Singleton instead, actually) and some would argue it should use other methods, some would even argue the <code>User</code> object should be passed infinitely through objects so it arrives where it needs to (which is an absolute nightmare to deal with).</p> <p>In reality, use them as you see fit. You are the developer. Figure out what works best for the project and for you and your team.</p> <p>Read <a href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9b.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7f6d" rel="nofollow">here</a> about constants and <a href="http://greenethumb.com/article/27/public-private-protected-internal-access-modifiers-in-as3/" rel="nofollow">here</a> for a good general overview about access modifiers in AS3 (and most class-based OOP languages in general)</p> <p>A <code>static const</code> offers benefits from both. It is an object that only exists once and is only set once, so it offers memory AND process time benefits. It is generally used when you need to save a value that will never change but needs to be accessed multiple times (like <code>Math.PI</code> or <code>MouseEvent.MOUSE_DOWN</code>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload