Passing arguments to constructors using CreateObject in Coldfusion
Asked Answered
R

1

6

I'm trying to use a Java object in Coldfusion using the CreateObject function.

This works fine when the constructor in the Java class doesn't take any arguments e.g.:

MyObject myObject = new MyObject();

Goes to

myObject = CreateObject("java",  "com.something.MyObject");

But I'm not sure how to pass arguments to the constructor for code like this:

MyObject2 myObject2 = new MyObject2(myArgment);

I'm sure this should be really simple, but I can't find this anywhere.

Thanks,

Tom

Razorback answered 22/12, 2009 at 15:37 Comment(0)
S
16

You can pass them in the init method like this:

myObject = CreateObject("java",  "com.something.MyObject").init(constructorArg1, constructorarg2);

Note that if you do NOT call init at all then you will be working with only static methods of the object and not an instance.

Spud answered 22/12, 2009 at 16:10 Comment(1)
Just to clarify, calling the init() method on a java object invokes that object's constructor. Init() is not a real method of the object and is used only to call the constructor: livedocs.adobe.com/coldfusion/8/htmldocs/Java_7.htmlShirty

© 2022 - 2024 — McMap. All rights reserved.