Be a Supporter!

As3: Static Keyword, The

  • 1,342 Views
  • 6 Replies
New Topic Respond to this Topic
Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
As3: Static Keyword, The 2012-03-18 15:40:39 Reply

AS3: Static Keyword, The
----------------------
AS3: Main
----------------------

What is "static"?
Static is a keyword found in many programming languages, including AS3. When writing AS3 you are limited to using it with only functions and variables.
So, for example, this is how you would create a static variable:

public class MyClass
{
    static public var foo:int = 42;
}

Since the variable foo has been declared as static there will only ever be one instance of it.
Even if you do this:

var a:MyClass = new MyClass;
var b:MyClass = new MyClass;
var c:MyClass = new MyClass;

There is still just a single instance of foo. Think Highlander: "There can be only one!"..
As well as only a single instance of it ever existing, you also access it like so:

var value:int = MyClass.foo;

But, that also means that you can't access it like so:

var myclassInst:MyClass = new MyClass;
var value:int = myclassInst.foo; //illegal

What about static functions?
Static functions are created the same way that static variables are:

public class MyClass
{
    static private var __foo:int = 42;
	
    static public function getFoo():int
	{
	    return __foo;
	}
}

And they are also accessed they same way as static variables:

var myclassInst:MyClass = new MyClass;

var value1:int = MyClass.getFoo(); //legal
var value2:int = myclassInst.getFoo(); //illegal

Static functions come with a caveat though: they can only access other static members.
This means that the following is invalid:

public class MyClass
{
    private var __foo:int = 42;
	
    static public function getFoo():int
	{
	    return __foo;
	}
}

Because __foo is no longer a static variable, the static function getFoo no longer has access to it.
However, the following is valid:

public class MyClass
{
    static private var __foo:int = 42;
	
    public function getFoo():int
	{
	    return __foo;
	}
}

A non-static function is capable of accessing a static variable.

When should I use static members?
Generally speaking, you don't need them very often. You should only use the static keyword if you know for a fact that you only need a single instance of a certain value.
Here are some examples of when you should use the static keyword:

Tracking Number of Instances
Say you want to track the total number of times an instance of a certain object has been created. And then, using that total number, which will be unique for each newly created object, assign a unique id to the object.
You can use static keyword to do that:

public class MyClass
{
    static private var __total:uint = 0;
	private const __id:uint = __total++;
}

Storing Colour Values
If you wanted to store a bunch of colour values, you could use the static keyword:

public class Colours
{
    static public const RED:uint = 0xFFFF0000;
	static public const GREEN:uint = 0xFF00FF00;
	static public const BLUE:uint = 0xFF0000FF;
}

In fact, this is how the event objects in AS3 are stored.
If you have ever wondered what Event.ADDED_TO_STAGE or Event.ENTER_FRAME is: they are just static strings.

trace(Event.ADDED_TO_STAGE); //Output: addedToStage
trace(Event.ENTER_FRAME); //Output: enterFrame

When should I not use static members?
Don't declare your members as static all willy nilly just because you can access them without creating an instance of the object.
Only use them if you need them. If you need to ask yourself "Do I need to use static here?" then you probably don't need static members.

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to As3: Static Keyword, The 2012-03-18 19:41:30 Reply

At 3 hours ago, Diki wrote: In fact, this is how the event objects in AS3 are stored.
If you have ever wondered what Event.ADDED_TO_STAGE or Event.ENTER_FRAME is: they are just static strings.

trace(Event.ADDED_TO_STAGE); //Output: addedToStage
trace(Event.ENTER_FRAME); //Output: enterFrame

This is something really cool to know, I never heard before that ENTER_FRAME and company were static. Let's say I would have a method for creating a line, how I could write something like this: Line.CURVED_LINE?

Attila0413
Attila0413
  • Member since: Dec. 10, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to As3: Static Keyword, The 2012-03-18 21:15:25 Reply

At 1 hour ago, Spysociety wrote:
At 3 hours ago, Diki wrote: In fact, this is how the event objects in AS3 are stored.
If you have ever wondered what Event.ADDED_TO_STAGE or Event.ENTER_FRAME is: they are just static strings.

trace(Event.ADDED_TO_STAGE); //Output: addedToStage
trace(Event.ENTER_FRAME); //Output: enterFrame
This is something really cool to know, I never heard before that ENTER_FRAME and company were static. Let's say I would have a method for creating a line, how I could write something like this: Line.CURVED_LINE?

The Event class just store them as static public constants:
http://help.adobe.com/en_US/FlashPlatform/reference/actionsc ript/3/flash/events/Event.html

Well, to do that just create a static const, like:
public static const CURVED_LINE : String = "curved";

And then the user will be able to pass the const as a parameter instead of writing the actual string. It's also good to prevent typos, as if the user fails to write the const correctly it will not compile.


BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to As3: Static Keyword, The 2012-03-19 03:28:10 Reply

You can also just use integer values.

EuPlonKa
EuPlonKa
  • Member since: Dec. 18, 2007
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As3: Static Keyword, The 2012-03-19 03:47:53 Reply

Its good to get the information about static out there. But without the context of Class vs Instance, it becomes meaningless to a beginner.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to As3: Static Keyword, The 2012-03-19 03:51:27 Reply

At 2 minutes ago, EuPlonKa wrote: But without the context of Class vs Instance, it becomes meaningless to a beginner.

Which is why Diki posted this example:

At 12 hours ago, Diki wrote: But, that also means that you can't access it like so:

var myclassInst:MyClass = new MyClass;
var value:int = myclassInst.foo;
Kirk-Cocaine
Kirk-Cocaine
  • Member since: Aug. 17, 2003
  • Offline.
Forum Stats
Moderator
Level 38
Programmer
Response to As3: Static Keyword, The 2012-03-19 08:00:31 Reply

At 16 hours ago, Diki wrote: When should I use static members?
Generally speaking, you don't need them very often. You should only use the static keyword if you know for a fact that you only need a single instance of a certain value.

Furthermore, you can use the static keyword to create a singleton if you know for a fact that you only need a single instance of a certain object.


The water in Majorca don't taste like what it oughta.

| AS3: Main | AS2: Main | Flash Tutorials |

BBS Signature