The No.1 i-Technology Magazine in the World !
   
 
Straxus
cat /dev/kbd | grep --random-lines > straxus.javadevelopersjournal.com
Ryan Slobojan (Straxus) is a
Software Consultant with
Codesta LLC
(http://www.codesta.com)
««
January 2009
»»
SM
T
WTFS
     123
45678910
11121314151617
18192021222324
25262728293031
Search
 

RSS2.0
ATOM
New entries
Comments
Mailing List




Actionscript classes, private vars, and unexpected static cling

Note: ActionScript is the Javascript-based language that Macromedia Flash uses. Macromedia Flex is an XML-based rapid application development platform - you create an .mxml page, and when a browser hits that page it is compiled by the server into a Flash movie (similar to how JSP and ASP work, except they [usually] compile to HTML). A FAQ is available if you would like more details.

I want to warn you about a crazy ActionScript behaviour I recently encountered while working with Macromedia Flex. It appears that an ActionScript class is different enough from a normal class (in e.g. Java or C#) that bizarre things can result if you attempt to use it like a regular class.

Take the following two example classes:
class PrivateVarClass
{
public function PrivateVarClass()
{
}

private var _localArray : Array = new Array();

public function get Pages() : Array
{
return this._localArray;
}
}

class ConstructorClass
{
public function ConstructorClass()
{
this._localArray = new Array();
}

private var _localArray : Array = null;

public function get Pages() : Array
{
return this._localArray;
}
}
These are two very similar (and very simple) classes, with one difference – PrivateVarClass declares _localArray as a new Array() in the var declaration, whereas ConstructorClass assigns a new Array() to _localArray in the constructor.

Now, consider the following example code which was embedded in a Macromedia Flex <mx:Script> tag:
var priv1 : PrivateVarClass = new PrivateVarClass();  var cons1 : ConstructorClass = new ConstructorClass();
var priv2 : PrivateVarClass = new PrivateVarClass(); var cons2 : ConstructorClass = new ConstructorClass();
var priv3 : PrivateVarClass = new PrivateVarClass(); var cons3 : ConstructorClass = new ConstructorClass();

priv1.Pages.push("1:1"); cons1.Pages.push("1:1"); // Do the same push operations
priv1.Pages.push("1:2"); cons1.Pages.push("1:2"); // on both sets of classes.
priv2.Pages.push("2:1"); cons2.Pages.push("2:1");
priv3.Pages.push("3:1"); cons3.Pages.push("3:1");
priv3.Pages.push("3:2"); cons3.Pages.push("3:2");
priv3.Pages.push("3:3"); cons3.Pages.push("3:3");

alert('priv1 Pages are ' + priv1.Pages.join() + ', priv1 Pages length is ' + priv1.Pages.length);
alert('cons1 Pages are ' + cons1.Pages.join() + ', cons1 Pages length is ' + cons1.Pages.length);
This code will create two parallel sets of classes, and do the same operations to both. It will output the contents of one of the object’s arrays at the end. However, when you run this code, you get the following output:

priv1 Pages are 1:1,1:2,2:1,3:1,3:2,3:3, priv1 Pages length is 6
cons1 Pages are 1:1,1:2, cons1 Pages length is 2

This means that if you assign an Object to a private instance variable in the variable declaration, the single instance of that variable is shared by all instances of the class, as if it was declared static. This is very much unlike Java, C#, or any other language I’ve encountered to this point, so I advise that all variable assignment occur within the constructor, NOT in the variable declaration.

This blog is created and maintained by the author of the page and in no way associated with SYS-CON Media or JDJ. The author of the blog assumes all liability and responsibility personally for the content of the page. JavaTM, J2EE, J2ME, J2SE, and other Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON and JDJ are independent of Sun Microsystems.
www.blog-n-play.com is a registered trademark (78553120) of SYS-CON Media.