Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / ActionScript Technology Center / Learning ActionScript 3 /

ActionScript 3 fundamentals: Variables

by Michelle Yaiser

Michelle Yaiser
  • Adobe
  • michelleyaiser.com

Content

  • Variable naming limitations and conventions
  • Access control attributes
  • Variable declaration
  • Data assignment
  • Constants
  • Where to go from here
  • Acknowledgement

Created

24 October 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript Adobe AIR Flash Builder Flash Player Flash Professional

Requirements

Prerequisite knowledge

You should be familiar with ActionScript 3 syntax.

User level

Beginning

Required products

  • Flash Builder (Download trial)
  • Flash Professional (Download trial)

Computers work by storing and operating on values in memory. While computers may be good at keeping track of an individual location storing one value or another, humans are not. Instead of keeping track of the location of these items in memory, developers assign a name to reference each unique thing stored. This name is called a variable. Any time the computer sees a variable name in your code, the computer looks in its memory and uses the value it finds there. For example, if you have two variables named firstVariable and secondVariable , each containing a number, to add those two numbers you write the statement:

firstVariable + secondVariable

If firstVariable contained 5 and secondVariable contained 43, the arithmetic above results in 48.

A memory location can only hold one value at any given time. Therefore, a variable only refers to one value at any given time. That value can vary as your program runs. If secondVariable changes to 23, summing the two variables would result in 28.

In this article, you will learn about the characters that can be legally used to name a variable. You will also learn to declare, assign a value to, and control access to your variable. Finally, you will learn about another way to reference memory that is not variable, but rather constant.

Variable naming limitations and conventions

Each variable must have a unique name. In ActionScript 3, you can name variables using words that remind you what the variables are. Once you properly name a variable, you only have to call the variable by name when you want to use it.

Each programming language has its own rules about naming variables. For example, some languages limit name length or prohibit certain characters or words. If you break one of these rules, an error occurs and your code does not run.

Naming limitations for variables in ActionScript 3

  • Variable names cannot be a reserved word or keyword. For more information, see ActionScript 3 fundamentals: Syntax
  • Variable names must start with a letter, an underscore, or a dollar sign
  • Variable names cannot use special characters (except _ and $ in certain situations)
  • Variable names must be unique

In addition to observing the limitations of the language, programmers also follow conventions when naming their variables. Some conventions are considered standard practices within a professional community and therefore have shared meaning. Managers, development teams, or clients sometimes dictate conventions. Programmers also develop naming conventions as part of their own personal coding style. Unlike failure to observe the naming limitations of a language, failure to follow conventions does not prompt errors. However, following naming conventions can actually be more important to a successful development process than observing the naming limitations.

Naming conventions for variables in ActionScript 3

  • Use camel case: Start with a lowercase letter and include uppercase letters, for example totalCost or dailyTotalsArray
  • Use an underscore ( _ ) only to start the name of a private variable
  • Use a dollar ( $ ) sign only to start the name of a static variable
  • Use descriptive variable names so that the content of any variable is obvious, no matter where the variable appears in code

Access control attributes

You control access to variables, classes, functions, and methods using access control attributes. Access control attributes help achieve encapsulation. Encapsulation is an important concept in object-oriented programming; for more information, see Object-oriented programming concepts: Encapsulation.

You use these access control attribute keywords when you declare variables to control access in ActionScript 3.

Table 1. Access control attribute keywords

Access control attribute keyword

Access

public

available to any caller

private

available only to the class that defines it

protected

available only to the class that defines it and to any subclasses of that class

internal

available to any caller within the same package

Variable declaration

Creating a variable is called variable declaration. ActionScript 3 requires the use of a keyword— var —to signal that you are declaring a variable. Including the keyword var , there are four parts to (non-local) variable declaration in ActionScript 3:

  1. access control attribute keyword
  2. var
  3. variable name
  4. data type

Here are some examples of variable declaration:

public var userName:String; private var _correctAnswer:Boolean; internal var userAge:uint; protected var predictedTemperature:int; private var _productPrice:Number;

For more information on data types, see ActionScript 3 fundamentals: Data types.

Data assignment

Putting data into a variable is called assignment. It uses the assignment operator = . Use the assignment operator with a variable name on the left and the data on the right. You can assign data anytime after a variable has been declared.

The following example declares a variable named userAge to hold an integer and then assigns it the value of 21.

internal var userAge:int; userAge = 21;

Because you can assign data anytime after a variable has been declared, you can do it immediately after declaration within the same statement.

public var userName:String = "Jane"; private var _correctAnswer:Boolean = true; internal var userAge:uint = 19; protected var predictedTemperature:int = -5; private var _productPrice:Number = 5.99;

Assignment can take place anytime after a variable has been declared—at declaration, many lines later, or after an event occurs. Assignment takes place when the data is known.

Constants

Constants are properties with a fixed value that cannot be altered. You can assign a value to a constant only once, and the assignment must occur in proximity to the declaration of the constant. An error results if you attempt to change the data in a constant.

Constants are declared using the const keyword instead of var . By convention, constant names are all caps with words separated by an underscore

Example: You are building an application that converts miles to kilometers. Miles are multiplied by a conversion value to get kilometers. If that conversion value changes, the conversions are no longer correct. Use a constant to hold the conversion value.

const MILES_KM_CONVERSION_VALUE:Number = 1.61;

Where to go from here

Variables are a method of communication used to represent storage locations for various items in memory. They are a central concept in programming and are used throughout every other aspect of programming.

When variables are combined with functions, they become parameters or local variables. When they are combined with objects, they can represent properties or data which an object stores or they can serve as a reference to the object itself. To learn more about each of these: ActionScript 3 fundamentals: Functions and Introduction to Objects (coming October 31).

Acknowledgement

The content in this article is based on material originally published in the Learning ActionScript 3 user guide created by Adobe Community Help and Learning.

More Like This

  • Garbage collection internals for Flash Player and Adobe AIR
  • ActionScript 3 fundamentals: Associative arrays, maps, and dictionaries
  • ActionScript 3 fundamentals: Namespaces
  • AS3 fundamentals: Conditionals
  • ActionScript 3 fundamentals: Arrays
  • ActionScript 3 fundamentals: Data types
  • ActionScript 3 fundamentals: Syntax
  • ActionScript 3 fundamentals: Numbers and Math
  • ActionScript 3 fundamentals: Functions
  • ActionScript 3 fundamentals: Packages

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement