After having seen the properties (variables inside an Actionscript 3.0 class), we can now introduce the constants.
The constants are a new characteristic implemented in the version 3.0. In fact, with the 2.0 they were not present.
For convention, they are declared using a name in capital letters so to understand at first sight that we are dealing with constants and not variables (properties).
We can define the value of a constant but we cannot modify it.
If we use more words as the constant name, it is of good rule to add an underscore in between the words.
Examples:
- Event.COMPLETE, COMPLETE is a constant of the Event class.
- MouseEvent.CLICK, CLICK is a constant of the MouseEvent class.
Let us look at it with even more examples'
I create a FLA and save it as 'ottava.fla'.
I create the Document Class associated to the FLA and save it as 'Ottava.as', implemented the following way:
Code:
package
{
import flash.display.MovieClip;
public class Ottava extends MovieClip
{
public function Ottava()
{
}
}
}
I will now declare the constant:
Code:
package
{
import flash.display.MovieClip;
public class Ottava extends MovieClip
{
private const NOME:String='Filippo';
public function Ottava()
{
}
}
}
private const NOME:String='Filippo';
- private :we do know by now the meaning of it
- const :declares to Flash that it is a constant and not a variable
- NOME :the constant's name in capital letter
- :String :is the type of value accepted by the constant (exactly as a variable)
- ='Filippo'; :the constant's value
We can access to the content of the constant in the same way as for the content of a variable:
Code:
package
{
import flash.display.MovieClip;
public class Ottava extends MovieClip
{
private const NOME:String='Filippo';
public function Ottava()
{
trace(NOME);
}
}
}
with trace(NOME);
I obtain the following output:
Filippo
It is not possible to assign a value to a constant after its declaration.
As an example, I declare another constant:
Code:
package
{
import flash.display.MovieClip;
public class Ottava extends MovieClip
{
private const NOME:String='Filippo';
private const ETA:int;
public function Ottava()
{
trace(NOME);
}
}
}
if I publish the SWF, Flash would return the following error:
Warning: 1110: The constant was not initialized.
It simply tells us that we did not assign a value to the constant.
In fact, if we try to assign a value from the building function:
Code:
package
{
import flash.display.MovieClip;
public class Ottava extends MovieClip
{
private const NOME:String='Filippo';
private const ETA:int;
public function Ottava()
{
trace(NOME);
ETA=35;
}
}
}
Flash would return the following error:
1049: Illegal assignment to a variable specified as constant.
It tells us that we are trying to assign a value to a constant in a forbidden way, meaning after the constant's declaration.
About the constant's attributes'
They are the same applied to the variables:
private, internal, protected, public and static
I recommend to look at tutorial 4 if you have any doubt about the variable's attributes.
Source files:
Bookmarks