Variable Modifiers in Flex vs ColdFusion CFC Scopes
Coming from ColdFusion, it can take a while to get your head around the different variable modifiers in Flex and what you use them for. I thought I'd devote some time to explaining them in relation to CFCs.
Here are the most common ones that you use:
- Public: A public variable, or method is one that can be accessed by any class using this one. In a CFC, this would be equivalent to the 'this' scope. For CFC methods, public is the default setting.
- Protected: Protected variables or methods in AS3 are only available within the class or in classes that extend from this one. In a CFC, this would be equivalent to the variables scope. For CFC Methods you can set the access to private.
- Private: Private variables, or methods, are only available in the class that defines it. Nothing from the outside, or a class that extends this one can access or change this variable. There is no parallel to this in ColdFusion CFCs. If you truly need something private, most people use the "this" Scope for variables or 'private' for methods. Is it confusing that CFC "Private" is really AS3 "Protected"? It might be.
- Internal: I'll admit these aren't all that common, but since there was an easy parallel I put it in the common list. In AS3, internal methods are accessible in the class and in other classes in the package. A package is just a bunch of classes in the same directory. My memory is that internal classes are available to subclasses too, although the documentation does not state that. I spoke about Internal methods in another post. In ColdFusion, you should use "access=package" For methods; however there is no parallel for variables; you're stuck with the this scope.
There are other access modifiers that you can use, but most of them don't have a parallel between ColdFusion and Flex. Let's talk about them:
- Remote: Remote is a ColdFusion method modifier. IT makes the method available for remote access, such as via a Web Service. This just doesn't apply to Flex code, because you won't be using AS3 to write an API for consumption by some remote consumer.
- static: Static variables or methods belong to the class. You call them on the class, not on an instance of the class.
- Final: Final tells you that a method cannot be overriden by subclasses. It is a "Buck stops here" command, so to speak.
- override: The override qualifer is used only in subclasses, and only for methods. It tells you Flex that this method is a replacement for the one in a parent class.
ColdFusion doesn't support any CFC level modifiers, but AS3 does support one, the dynamic attribute. You'll use dynamic if you want to add properties to the class at runtime. To be honest, I've never had the need for this in a production environment; can anyone expand on the use case for this?
I feel like I've been doing a lot of "list" posts lately, so next time I resolve to get back to code!
Also a quick reminder, there are only a few more days to apply for the DotComIt focus group. You can read more about it here or just jump right to the survey here.




[start quote]
err.. sorry, make that <i>public static final</i> .. which AS3 doesn't handle too well ;-)
[end quote]
And I changed the text of this blog post to specify that final was only for methods.