ADOBE FLEX 3
You create an instance of a class by using the new operator. The following example creates an instance of the Date
var myBirthday:Date = new Date();
Packages and namespaces
Packages and namespaces are related concepts. Packages allow you to bundle class definitions together in a way that
facilitates code sharing and minimizes naming conflicts. Namespaces allow you to control the visibility of identifiers,
such as property and method names, and can be applied to code whether it resides inside or outside a package.
Packages let you organize your class files, and namespaces let you manage the visibility of individual properties and
Packages in ActionScript 3.0 are implemented with namespaces, but are not synonymous with them. When you
declare a package, you are implicitly creating a special type of namespace that is guaranteed to be known at compile
time. Namespaces, when created explicitly, are not necessarily known at compile time.
The following example uses the
package directive to create a simple package containing one class:
public class SampleCode
public var sampleGreeting:String;
public function sampleFunction()
trace(sampleGreeting + " from sampleFunction()");
The name of the class in this example is SampleCode. Because the class is inside the samples package, the compiler
automatically qualifies the class name at compile time into its fully qualified name: samples.SampleCode. The
compiler also qualifies the names of any properties or methods, so that
sampleGreeting and sampleFunction()
samples.SampleCode.sampleGreeting and samples.SampleCode.sampleFunction(), respectively.
Many developers, especially those with Java programming backgrounds, may choose to place only classes at the top
level of a package. ActionScript 3.0, however, supports not only classes at the top level of a package, but also variables,
functions, and even statements. One advanced use of this feature is to define a namespace at the top level of a package
so that it will be available to all classes in that package. Note, however, that only two access specifiers,
internal, are allowed at the top level of a package. Unlike Java, which allows you to declare nested classes as private,
ActionScript 3.0 supports neither nested nor private classes.
In many other ways, however, ActionScript 3.0 packages are similar to packages in the Java programming language.
As you can see in the previous example, fully qualified package references are expressed using the dot operator (
just as they are in Java. You can use packages to organize your code into an intuitive hierarchical structure for use by
other programmers. This facilitates code sharing by allowing you to create your own package to share with others,
and to use packages created by others in your code.