Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

ActionScript for Java developers, Part 2

Properties, dynamic behavior, and functions in ActionScript 3

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (4)
Login
Forgot your account info?

below By bastaboy on January 22, 2010, 7:14 ampaper writing service

Reply | Read entire comment

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

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • "ActionScript for Java developers, Part 1" (Chet Haase, JavaWorld, February 2009) is a guide to the basic differences in class structure and syntax between the two languages.
  • "Java EE and Flex: A compelling combination" (Dustin Marx, JavaWorld, January and February 2009) introduces techniques for integrating Flex clients with a Java back end. Part 2 focuses on connecting a Flex client and Java enterprise back-end via SOAP-based Web services, object remoting, and BlazeDS.
  • "Client-side Java's evolutionary leap" (Jeff Friesen, JavaWorld, January 2009) looks back on 2008 as a momentous year for Java desktop and RIA technologies, with commentary from many leaders and innovators in client-side development.
  • "The ABCs of RIA" (Paul Krill, Infoworld, August 2007) is a survey of tools and frameworks used in rich Internet application development.
  • ActionScript Developer Center is the Adobe developer site for the language.
  • The Flex SDK Open Source site is the open source repository for the Flex libraries. You can download the latest release of Flex and build applications with it, or download the source code and build Flex from scratch. Also check out learning resources such as the Flex Developer Center site and Flex.org.
  • Flex Builder is Adobe's IDE for building Flex applications. The Flex SDK is free to use, but tools can make development easier.
  • The Flash Development Center provides resources for developers writing programs for the Flash platform, which can be done either with Flex, or by using the Flash APIs directly, or with the Flash authoring tool.
  • Filthy Rich Clients (Chet Haase and Romain Guy; Addison-Wesley/Sun Microsystems Press, August 2007) is a definitive guide to client-side development in Java.
  • Chet Haase regularly posts graphics, animation, and tutorial articles and videos to his technical blog Codedependent, and irregularly posts comedy writings to his non-technical blog Enough About You.

More from JavaWorld