Pup
Pup

Reputation: 10516

What values are safe to use to initialize class definition's static and const members?

Phrased differently, this question could read, "What is the order of compile-time variable declarations and definitions?"

I can't recall specific examples at the moment, but I know I've run into trouble when initializing const and static values in my class definitions due to the declaration of those values occurring out of order.

I know I can instantiate objects when declaring static const members, like so:

public class ConstsWithNewObjects {
    public static const DEFINED_NOW_2:Object = {something:"Defined!"};
    public static const DEFINED_NOW_3:Object = new CustomObject("Defined!");
}

But, if I'm accessing one of those members from another static or const value, I imagine race conditions arising, like so:

public class ConstsWithOtherConsts {
    public static const DEFINED_NOW_1:Object = DEFINED_NOW_3; // Does this exist, yet?
}

Upvotes: 0

Views: 205

Answers (1)

JonnyReeves
JonnyReeves

Reputation: 6209

ActoinScript Hero Jack Dunstan has covered this exact topic in great detail on his blog: Class Bootup Part 2.

Regardless of Jack's findings; I would recommend, for the sanity of your readers; that you keep your static initialiser code as clean, and simple as possible - don't forget that static fields can call static methods; you can also make use of a static Class initialiser method as well.

Upvotes: 1

Related Questions