How ColdFusion CreateObject() Really Works With Java Objects

Posted May 30, 2007 at 10:06 AM

Tags: ColdFusion

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 »

  • <!--- Get the string buffer class object. --->
  • <cfset objStringBuffer = CreateObject(
  • "java",
  • "java.lang.StringBuffer"
  • ) />
  •  
  • <!---
  • From the string buffer class object, instantiate
  • two sepparate instances of the Java string buffer.
  • --->
  • <cfset objDataOne = objStringBuffer.Init() />
  • <cfset objDataTwo = objStringBuffer.Init() />

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:


 
 
 

 
ColdFusion CreateObject() Functionality For Java  
 
 
 

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

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

May 30, 2007 at 10:43 AM // reply »
76 Comments

Thathow I understood it to be after Rupesh's and Sean's comments.


May 30, 2007 at 10:46 AM // reply »
7,507 Comments

Awesome. Thanks for your help and feedback.


Nov 20, 2007 at 12:42 PM // reply »
1 Comments

You can search "Cold" word in http://searchcode.tm-sol.com/. might give you some examples also.

found good examples as well.


Feb 10, 2010 at 12:53 PM // reply »
1 Comments

How do you free the memory used by this object on the coldfusion server?


Feb 10, 2010 at 10:06 PM // reply »
7,507 Comments

@Dan,

You don't have to - the garbage collection will handle that for you.


Mar 9, 2010 at 10:16 AM // reply »
3 Comments

you can also create WebService objects this way.

i.e.:
<cfscript>
dynamicsWS = CreateObject("webservice", "http://192.168.0.100/DynamicsGPWebServices/DynamicsGPService.asmx?WSDL");
</cfscript>

<cfinvoke webservice="#dynamicsWS#" method="GetCompanyList" refreshWSDL="yes" timeout="30" returnvariable="result">
</cfinvoke>

This creates WebService object in your ColdFusion instance (available from administration panel -> webservices) and this object "lives" in there for about 10 (gets deleted if not used).

So I never took CreateObject() for granted :)


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 15, 2010 at 9:21 PM
Tim Cracked The GMail - CFMailPart Puzzle!
@Bruce Holm, > So I think sending two parts is a minimum. > Giving the user the option of opting for > text only saves bandwidth [and storage] How does sending two cfmailparts with bot ... read »
Mar 15, 2010 at 9:02 PM
Tim Cracked The GMail - CFMailPart Puzzle!
I should have added that about 5% of our database of opt-in accounts in the past couple years have chosen Text. The default is HTML email if they don't choose. ... read »
Mar 15, 2010 at 8:52 PM
Tim Cracked The GMail - CFMailPart Puzzle!
Ben, From my research there are reasons for sending out email with a text content part in them because there are still some folks who only want text email. For one it's their way of filtering out spa ... read »
Mar 15, 2010 at 6:02 PM
FLEX On jQuery: Decouple Components With Event Listeners
@Banned, When you write it in FLEX, it probably has hundreds, if not thousands, of lines of code running... it's just that someone else wrote most of it for you :) Not to say you don't reap tremend ... read »
Mar 15, 2010 at 5:48 PM
Ask Ben: Finding The SQL Data Type Of A ColdFusion Query Column
Hi Ben, This solved a major problem we had with an app. We were using the cfdbInfo tag to get just the column names and dataTypes. It was taking (for some, unknown to me, reason) 2-3 minutes per t ... read »
Mar 15, 2010 at 5:46 PM
FLEX On jQuery: Decouple Components With Event Listeners
Whew, that's a lot of JavaScript code to maintain! Although I like the direction you're going here (and have used similar techniques in the past), this seems excessively code-intensive to me. As ... read »
Mar 15, 2010 at 4:16 PM
AxisFault: ColdFusion Web Services And XML Data Types
@Charles, No problem :) ... read »
Mar 15, 2010 at 4:15 PM
AxisFault: ColdFusion Web Services And XML Data Types
yes this help. thanks again. ... read »