AS3 TypeError: Error # 1007: attempt to create objects on a non-constructor

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

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

It compiles, but throws the first access to SomeClass :

 TypeError: Error #1007: Instantiation attempted on a non-constructor. at com.domain::SomeClass$cinit() ... 
+7
source share
4 answers

+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(); } } 
+1
source

A non-constructor error is an inconvenient way to compile: "You called the constructor for a class that I have not seen"; if he was a little smarter, he could check the file (compilation unit) for inner classes before complaining ... mehhh

Seeing that you have provided your private static variable, it is obvious that you intend to use the instance only inside SomeClass (assumption can be passed as a return value).

The following solution discards the creation of a static var when the inner class is initialized, i.e. when the (supposedly implicit) Helper.cinit() , and not SomeClass.cinit() when the 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(); } 
+2
source

I think it cannot work with Helper and SomeClass in the same file. When SomeClass is initialized, the Helper class is not yet initialized, so the Helper object cannot be created.

Moving the assistant to a separate file should solve the problem.

+1
source

you need to generate a constructor and declare a variable in the class, not a function:

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

All Articles