We help IT Professionals succeed at work.

Creating class-wide ("package") variables with AS 2.0

Last Modified: 2011-10-03
Let's say I have an AS2.0 class called myClip, and in my calling script I invoke multiple instances of myClip() objects. Is there any way, within the class itself, to keep track of how many myClip objects have been created?

Currently, I'm doing this in the calling script, by just creating a variable which I increment each time a new myClip is created. But that would cause problems if I destroy a clip and would want to then decrement that value.

It would be so much more convenient if I could create a variable that's "global" across all instances of the class, like the VB "shared" accessor. I believe AS3.0 allows "package" variables which are accessible across the whole package, is that right?

At any rate, is there any way to do this in AS2?

Watch Question

//give the class a 'static variable:
class myclass{
    //static variable declaration
    private static var _instances = 0;

    //public access to variable
    public function instances(){
        return _instances;

    //then in your class constructor, increment this variable
    public function myclass () {
         //increments here for each new object

//access the value like so

//should work
Unlock this solution and get a sample of our free trial.
(No credit card required)


Good stuff. Thank you! Static vars work in AS2 just like in AS3, as it turns out, except there's no package definition, I guess.
Actually they do have packages in AS2, they're just not required the way they are in AS3. In AS3, if you don't put your classes in a package, an unnamed package is created at compile time to hold all unpackaged classes. Glad I could help.


Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.