Like a lot of newer languages, ActionScript 3 is different from Java when it comes to properties, dynamic behavior, and some very convenient aspects of functional programming. In this second half of his Java developer's guide to ActionScript 3, Chet Haase uses side-by-side code samples to demonstrate the differences in syntax and behavior. He also talks about capabilities and usage patterns that could come as a surprise, if you're viewing ActionScript through Java-tinted glasses. Level: Intermediate
I hope you got a chance to read the first half of this article, which introduced the topic of ActionScript 3 from a Java developer's perspective. In that article I gave an overview of ActionScript 3 and how it fits into the Flash platform and Flex development, and then looked at some of the differences in syntax and class structure between the two languages. If you're new to Flex development or ActionScript, Part 1 is good background for what you'll learn here. If you didn't get a chance to read it yet, you might want to do so now.
Go ahead, I'll wait.
Okay, ready? Then let's get started.
In this installment, I'll introduce some of the areas where we begin to see a striking, and more conceptual, difference between programming in Java versus ActionScript 3 (AS3, from now on) namely:
How properties are declared and used is one of the biggest differences between Java and ActionScript. In this section, you'll see how properties differ between the two languages at the API level, and how that changes the way they are declared and used.
Java has fields in classes, which can be public and directly settable/gettable:
public class JavaProps {
public int blah;
}
JavaProps props = new JavaProps();
props.blah = 5;
Good Java coding practice uses optional JavaBeans conventions for getting and setting fields, which are then declared as private:
public class JavaProps {
public int blah;
private int foo;
public void setFoo(int value) {
foo = value;
}
public int getFoo() {
return foo;
}
}
JavaProps props = new JavaProps();
props.blah = 5;
props.setFoo(5);
AS3 has properties as a first-class citizen of the language. In particular, there are get and set keywords that define special getters/setters for properties. For example, you could have either
public class AS3Props {
public var foo:int;
}
or
public class AS3Props {
private var _foo:int;
public function set foo(value:int):void {
_foo = value;
}
public function get foo():int {
return _foo;
}
}
In either case, callers would access the property using standard "field" access:
AS3Props props = new AS3Props();
props.foo = 5;
trace("foo = " + foo);
// outputs '5'
This equivalence in AS3 between fields and get/set functions has some interesting and profound implications, especially for developers writing APIs. (That explains my particular interest in this subject, by the way: I work on the Flex SDK team developing the Flex APIs.) For example, changes between the two representations have no impact on the public API since the calling mechanism for both is the same. As a result, you can evolve your API over time between the two options without causing problems for your users.
Let's see how this works in an example.
hiBy Anonymous on August 24, 2009, 1:32 pmC:\Documents and Settings\sarveshreddy\Local Settings\Temp
Reply | Read entire comment
Fabulous overviewBy Anonymous on June 14, 2009, 10:40 amThe content is very useful, but I think the author should give up computers and go into one of two professions: Either 1) writing, because the whole article felt...
Reply | Read entire comment
jsonBy Anonymous on March 19, 2009, 6:11 amIn java you can also do something like JSON FooObject foo = new FooObject(){{blah=5; mega="kuku"; huhu=10; setSomething("blabla");}} but it is not so nice
Reply | Read entire comment
View all comments