views:

34

answers:

2

I'm wondering about the effectiveness, cost of, or resources used to call a public static const from a class

Let's, hypothetically, say I have a class that has quite a few resources and calling the constructor is about 40kb of memory.

Is there any difference in adding static constants to the same class as opposed to creating a small class with just the constants?

In the case of an event dispatcher, the class listinging to the event would have something like this addEventListener(myClass.Holla,onHolla); or addEventListener(myClassEventNames.Holla,onHolla);

Is there a difference (significant enough) to warrant using an extra class to store the event names?

+2  A: 

No. There will be no significant difference.

Lee
While I do assume that this is the case, can you elaborate why this is so or have any links that support this?
Daniel
+1 Lee is 100% right, you can do performance profiling yourself, but is pretty logical, there is very little data (in bytes) to defining a class definition. I don't think you will find existing stats on this though.
Tyler Egeto
+1  A: 

Accessing a static member has nothing to do with the constructor; so you won't have any performance issues.


If the event in question is a custom event, the convention is to declare the string constants for all events of a particular class of event (subclass of flash.events.Event) in that event subclass itself. For example, all mouse event constants are declared in MouseEvent, all menu related events are defined in MenuEvent etc.

This convention will help you in code-completion if you're using the Flex mxmlc compiler. Let's say you have added the following metadata tag on top of a class definition (MyClass).

[Event(name="randomEvent", type="com.domain.events.CustomEvent")]
public class MyClass extends EventDispatcher { }

Now you declare an instance of this class and type in addEventListener:

var myClass:MyClass = new MyClass();
myclass.addEventListener(

You'll get CustomEvent.RANDOM_EVENT in the autocomplete dropdown list. Without the metadata tag, it will just give you the two default items (activate and deactivate). The metadata tag tells the compiler that this class dispatches an event of class CustomEvent and type randomEvent - and the compiler assumes that the string constant is defined as per convention and gives you CustomEvent.RANDOM_EVENT as the option.

Auto-completion might still work if you declare string constant in SomeOtherClass and provide the name of that class in the metadata tag - but that would be misleading as the class in question does not dispatch any event of SomeOtherClass

Amarghosh