Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
New Contributor
CanadianMike
Posts: 7
Registered: ‎02-25-2011
My Device: Not Specified

Use of Constants

I have a few questions about using constants....

 

1.  Are constants (private static const xxxx) compiled by reference of by value?

2.  Does using a class to contain constants(with multiple instantiation across classes) have an impact on memory use above just having the constants declared as global variables in each class?

3.  Is there a more effcient way of dealing with constants like a resource file for example, that compiles by value?

 

Perhaps this is better asked in an AIR Forum?

Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Use of Constants

Variable types like ints, Strings, etc are by value.

When a variable, class or function is static, there is only one reference to it regardless of the number of times the parent class is instantiated.

private/public static const is the easiest and most common way to do this. If it is coming from a resource file, it does not sound very constant and then you have to deal with loading the file, parsing, setting, defaults, etc.
Please use plain text.
New Developer
killerspaz
Posts: 199
Registered: ‎12-29-2010
My Device: Not Specified

Re: Use of Constants

1) Constants are neither defined as by reference or by value. TYPES are defined this way. Immutable objects are passed by value, where mutable objects are passed by reference. This is typical in most non-native languages: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9...
2) No, they are essentially the same. The issue is that if using static variables you're initializing them on launch of your app. If you're embedding assets into these constants, or many complex objects, you're consuming more memory than you probably desire.
3) A more efficient approach would be to have some sort of asset manager that will load objects from the filesystem or internet as necessary. i.e., load images on the fly, instead of storing them all in a single resource class. Once loaded, you might want to consider approaches for cleaning up after yourself as well. If a user can potentially load hundreds of images, you want to remove old images from memory to avoid stack overflows.
Please use plain text.