Introspection and private namespaces in Flex/AS3


Today, we are going to discuss introspection and private namespaces in AS3/Flex!

The motivation to use private namespaces in the context of introspection is to be able to expose API implemented in your class in a way that is not public (everyone can use it), protected (only inheriting classes can use it) or private (only the class can use it).

Consider this: You are implementing a framework, and one of the classes (ClassA) is used both by the framework users (external) and within the framework (internal). That means that all the functionality in ClassA that is intended for external use must be public. Unfortunately, so does the functionality intended for internal use.

If you would like to prevent exposing the internal API to the outside world, but still use it internally, private namespaces is the way to go!

Basically, these are the steps to use private namespaces:
1. Define the namespace:

namespace nameSpace;

2. Use the namespace instead of an access control specifier in a variable or function declaration:

namespace function myFunc() {}

3. Now, use it:

use namespace namespace;

So, without further ado, let’s get down to business.

First, we’ll create the namespace (file namespace/

package namespace {
// URIs default to {package}.{namespace defined in this file}, I am overriding this here.
public namespace nameSpace = "com.flexblackbelt.private.namespace";

Second, we’ll create the ClassA mentioned above (file example/

package example {
   import namespace.nameSpace;

   public class ClassA {

      nameSpace function func1():void {}
      nameSpace var tempVar:Object;
      public function func2():void {}


Now, any class ClassB that uses ClassA will be able to access func2.
But, to access func1 and tempVar, ClassB would have to import the namespace and use it:

Import namespace.nameSpace;
use namespace nameSpace;

I have created an example, you can download it here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: