I use ColdFusion's CreateObject() method at least a dozen times each day to create wonderful Java objects like String Buffers and File Input Stream. Until recently, I have never really thought about how it actually works - I have just accepted that the black magic under the hood did the job properly. But then, just the other day, I discovered that you could instantiate multiple Java instances off of the same Java class returned by CreateObject():
Launch code in new window » Download code as text file »
When I saw this, I realized that I had no idea how ColdFusion's CreateObject() functionality really worked. After some posting and some back and forth comments, Sammy Larbi, Rupesh Kumar, and Sean Corfield have really helped to clear things up:
| | | | ||
| | ![]() | | ||
| | | |
Apparently, when you call ColdFusion's CreateObject() method to create a Java object, ColdFusion is returning a proxy object to that Java object, not the Java object itself. This proxy object allows you to reference static properties and invoke static methods of that Java class without actually having to instantiate the full Java class. This is good for memory usage and for performance (object instantiation is a costly process).
If you call Init() on that proxy object, it then instantiates the Java object with the given arguments and returns a proxy object wrapped around this new instance (I suspect from the Init() example above that it must create a new proxy object otherwise you probably wouldn't be able to call Init() twice on the same ColdFusion proxy). Similarly, if you attempt to call a non-static method of the Java class, the proxy object will, behind the scenes, call Init() on the Java class (passing no arguments), and use that as the target class going forward.
Of course, this is what I think I understand. This may contain errors and if I get feedback on this, I will update the graphic as necessary. Thanks to all who helped me get this far.
Download Code Snippet ZIP File
Comments (3) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Storing Float Values In An Integer Column Of A ColdFusion Query
ColdFusion 8 Beta Released - ColdFusion 8 Release Date Contest Still Open
Thathow I understood it to be after Rupesh's and Sean's comments.
Posted by Sam on May 30, 2007 at 10:43 AM
Awesome. Thanks for your help and feedback.
Posted by Ben Nadel on May 30, 2007 at 10:46 AM
You can search "Cold" word in http://searchcode.tm-sol.com/. might give you some examples also.
found good examples as well.
Posted by Ashish on Nov 20, 2007 at 12:42 PM