ActionScript for Java developers, Part 2

Properties, dynamic behavior, and functions in ActionScript 3

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:

  • Properties: Some of the more interesting differences between Java and AS3 involve how properties are declared and used.
  • Dynamic behavior: Scripting languages are known for their dynamic behavior, or the ability to change classes, functions, and structures on the fly. AS3 is no stranger to this feature, and I'll show you how it fits into the language.
  • Functions: Java calls them methods, ActionScript calls them functions, but they're functionally equivalent. Still, there are some important differences to be discovered.

A proper tease on properties

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.

Fields versus properties

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.

1 2 3 4 5 Page
Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more