ActionScript for Java developers, Part 1

Learn how ActionScript 3 differs, and doesn't, from Java

1 2 Page 2
Page 2 of 2

The next level: Class behavior

As you've seen by now, AS3 classes look pretty much like Java classes. You haven't seen them yet, but AS3 interfaces also look eerily similar to their Java counterparts. But looks aren't everything (except with supermodels and wax fruit) and there are a few important distinctions in behavior that are worth investigating.

Constructors and permissive behavior

Java allows the same access permission specifiers (public, protected, private, and package-private) on constructors that are allowed on classes, fields, and methods:

 
 public class FooObject {
    private FooObject() {}
  }
  FooObject foo = new FooObject();
  // Compiler error

In AS3, constructors are always public:

public class FooObject {
    private function FooObject() {}
  }
  // Compiler error

Making a constructor private (in a language that supports it, like Java) is not a typical pattern, although it is helpful in some situations, like creating singletons. If you really want only one of something, then it's a good idea to prevent anyone but the class itself from creating it. A workaround used in AS3 involves throwing exceptions from the constructor when called from outside of your singleton accessor, but it is not quite the same thing.

Interfaces and properties

Java allows properties to be declared on interfaces:

public interface IFoo {
    public int blah = 5;
    public final int VAL = 7;
  }

*Both of these properties are implicitly static and final, even though they lack those keywords. Try it, you'll see.

AS3 does not allow properties on interfaces. Only functions can be declared on interfaces. Note, however, that you can declare properties with set/get functions; just not properties as fields. For example, this works:

public interface IProperties
  {
      function set blah(value:Number):void;    
      function get blah():Number;    
  }

If the get/set example here makes no sense, don't worry. You'll learn more about properties and these functions in the second half of this article.

Abstract, what's that?

Java allows abstract classes:

public abstract class FooObject {
    public abstract void foo();
  }

AS 3 ... does not. There is no concept of "abstract" in AS3.

Until next time ...

That's probably enough comparison to get a busy mind spinning, so I'll stop for now. I hope the examples here have whet your appetite, because there's more to come. In the second half of this article we'll get into the advanced topics of properties, dynamic behavior, and functions, with a similar line-up of code-based comparison and usage commentary from, well, me.

Acknowledgements

Thanks toJames Ward of Adobe for helping with the original presentation that these articles are based upon. Thanks also to Jeff Dyer, an actual language expert at Adobe, for providing a timely technical review and ensuring that I wasn't simply rattling off a bunch of half-baked opinions and outright stinking lies.

Chet Haase is a senior computer scientist on the Flex SDK team at Adobe Systems, where he spends most of his time working on animation and graphics-related technologies. He is the co-author of the Java book Filthy Rich Clients about graphics effects programming and the author of the humor book When I am King..., which has nothing whatsoever to do with Java, graphics, ActionScript, programming, or anything else.

Learn more about this topic

  • "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.
  • "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

1 2 Page 2
Page 2 of 2