Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 TypeError: Error #1007: Instantiation attempted on a non-constructor

For some reason I can't get this to work (heavily simplified code that fails):

package com.domain {
    public class SomeClass {
        private static var helper:Helper = new Helper();
    }
}

class Helper {
}

It compiles, but throws upon first access of SomeClass:

TypeError: Error #1007: Instantiation attempted on a non-constructor.
    at com.domain::SomeClass$cinit()
    ...
like image 409
Blanka Avatar asked Jun 13 '12 07:06

Blanka


2 Answers

The non-constructor error is the compiler's awkward way of saying 'you have called a constructor for a class I have not seen yet'; if it were a bit smarter, it could check the file (compilation unit) for internal classes before complaining... mehhh

Seeing as you have given your static variable private access, obviously you intend to only use the instance internally to SomeClass (assumption; could be passed out as a return value).

The following solution defers creation of the static var to when the internal class is initialized i.e. when the (presumably implicit) Helper.cinit() is invoked, rather than SomeClass.cinit() when Helper does not exist yet:

package com.domain {
    public class SomeClass {

        public function doSomething(param:*):void {
            // ... use Helper.INSTANCE
        }

    }
}

class Helper {
    public static const INSTANCE:Helper = new Helper();
}
like image 185
Darren Bishop Avatar answered Sep 20 '22 12:09

Darren Bishop


+1 to Darren. Another option is to move the Helper class to the top of the file

class Helper {
}

package com.domain {
    public class SomeClass {
        private static var helper:Helper = new Helper();
    }
}
like image 38
BlueRaja - Danny Pflughoeft Avatar answered Sep 20 '22 12:09

BlueRaja - Danny Pflughoeft