I think there's Ecmascript history to the actual answer and I'm not enough of a 
language guy to offer the "official" answer, but basically, classes are 
initialized on-demand as they are used by the code.  I think you ran into 
trouble in the setting of the const, not in the construction of the internal 
class

From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of 
cadisella
Sent: Wednesday, December 10, 2008 11:54 AM
To: [email protected]
Subject: [flexcoders] Re: inner classes and static initialization


Thank you, that works. I'm guessing then that the inner classes are
treated as globals in the same way that the static data members are, and
initialzed after the static data members? It seems that all globals
would be initialized before the class itself, as the class itself may
(and in my case, did) require the use of them.

This is coming from a Java background as well, where that seems to be
the case.

--- In [email protected]<mailto:flexcoders%40yahoogroups.com>, Alex 
Harui <[EMAIL PROTECTED]> wrote:
>
> The definition doesn't exist at static initialization time so I think
that's expected behavior. You'll see all of our code doing a null test
then creating the instance inside of getInstance()
>
> From: [email protected]<mailto:flexcoders%40yahoogroups.com> 
> [mailto:[email protected]<mailto:flexcoders%40yahoogroups.com>]
On Behalf Of cadisella
> Sent: Tuesday, December 09, 2008 1:50 PM
> To: [email protected]<mailto:flexcoders%40yahoogroups.com>
> Subject: [flexcoders] inner classes and static initialization
>
>
> I'm having a strange problem with the use of an inner class and I
wanted to see if anyone thought that this was a bug or the correct
behavior. Here's my test that shows the problem:
>
> package
> {
> use namespace MyNamespace;
>
> public class InternalClassTest
> {
> public static const mtInstance:InternalClassTest = new
InternalClassTest();
> public static function getInstance():InternalClassTest {
> return mtInstance;
> }
>
> MyNamespace var mtInternalClass:MyInternal;
>
> ! public function InternalClassTest()
> {
> mtInternalClass = new MyInternal();
> }
>
> }
> }
>
> use namespace MyNamespace;
>
> internal class MyInternal {
>
> }
>
> The existence of the static constant that calls the InternalClassTest
constructor is where the issue lies. I get this error:
>
> TypeError: Error #1007: Instantiation attempted on a non-constructor.
> at
InternalClassTest()[C:\5DCVS_Flex\Testing\src\InternalClassTest.as:16]
> at InternalClassTest$cinit()
> at! global$
init()[C:\5DCVS_Flex\Testing\src\InternalClassTest.as:5]
> at TestApp()[C:\5DCVS_Flex\Testing\src\TestApp.as:35]
> at Testing()[C:\5DCVS_Flex\Testing\src\Testing.mxml:0]
> at _Testing_mx_managers_SystemManager/create()
> at
mx.managers::SystemManager/initializeTopLevelWindow()[E:\dev\3.1.0\frame\
works\projects\framework\src\mx\managers\SystemManager.as:2454]
> at mx.managers::S!
ystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler\
<http://www.adobe.com/2006/flex/mx/internal::docFrameHandler>()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\managers\SystemMana\
ger.as:2344]
>
> If I remove the call to new MyInternal() from the constructor of
InternalClassTest, then I can get the same functionality by implementing
the internal class in the following way:
>
> internal class MyInternal {
> {
> InternalClassTest.getInstance().mtInternalClass = new
MyInternal();
> ! ; & nbsp; }
> }
>
> So the problem seems to be that the MyInternal class has not been
initialized when accessing the InternalClassTest class through a static
method. The solution is kind of a pain, and certainly not exactly
intuitive. From what I can tell it means that no singleton would be
able to access an internal class through its constructor.
>
> This is in Flex Builder 3.0.1. with the Flex 3.1 SDK.
>
> Thanks for any thoughts.
>
> Dale
>

Reply via email to