# AS2 arrays in a class instance

**URL:** <https://forum.kirupa.com/t/as2-arrays-in-a-class-instance/312457>\
**Category:** flash\
**Created:** [August 3, 2010, 9:36pm UTC](https://forum.kirupa.com/t/as2-arrays-in-a-class-instance/312457 "2010-08-03T21:36:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Poco](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@Poco](https://forum.kirupa.com/u/Poco)\
**Post date:** [August 3, 2010, 9:36pm UTC](https://forum.kirupa.com/t/as2-arrays-in-a-class-instance/312457/1 "2010-08-03T21:36:37Z")

</div>

Hi guys,

Today I found a thing that really bugs me. Let’s say I have this very basic class called Test which basically just creates an array and stores some objects in it:

```auto

class Test {
    private var _arr:Array = [];
    
    function Test() {}
    
    public function addToArray(param) {
        _arr.push(param);
    }
    
    public function getArrayLength() {
        return _arr.length;
    }
}

```

Now I create two instances of Test; I will add 2 strings to the first one, then I create the second instance:

```auto

//code on root
var obj1 = new Test();
obj1.addToArray("one");
obj1.addToArray("two");
trace("For obj1: "+obj1.getArrayLength());

var obj2 = new Test();
trace("For obj2: "+obj2.getArrayLength());

```

Now I would expect to get length 2 for the first object, lenght 0 for the second, however it is not so. Seems like the second object is using the very same array, so it traces the following:  
For obj1: 2  
For obj2: 2

My question - is this a bug or am I missing something?

I tried the same in AS3, there it works as I expected:  
For obj1: 2  
For obj2: 0  
Code is the very same, I am not copying it here cos it has no sense to do so.

Thanks for advices!

Cheers,  
Poco
