User Guide

Table Of Contents
930 Chapter 37: Integrating J2EE and Java Elements in CFML Applications
If the method has one or more arguments, put the arguments in parentheses, separated by
commas, as in the following example, which has one integer argument and one string
argument:
<cfset x = 23>
<cfset retVal = obj.Method1(x, "a string literal")>
Note: When you invoke a Java method, the type of the data being used is important. For more
information see “Java and ColdFusion data type conversions” on page 933.
Calling JavaBean get and set methods
ColdFusion can automatically invoke getPropertyName() and setPropertyName(value) methods if a
Java class conforms to the JavaBeans pattern. As a result, you can set or get the property by
referencing it directly, without having to explicitly invoke a method.
For example, if the myFishTank class is a JavaBean, the following code returns the results of
calling the getTotalFish() method on the myFish object:
<cfoutput>
There are currently #myFish.TotalFish# fish in the tank.
</cfoutput>
The following example adds one guppy to a myFish object by implicitly calling the
setGuppyCount(int number) method:
<cfset myFish.GuppyCount = myFish.GuppyCount + 1>
Note: You can use the direct reference method to get or set values in some classes that have
getProperty and setProperty methods but do not conform fully to the JavaBean pattern. However,
you cannot use this technique for all classes that have getProperty and setProperty methods. For
example, you cannot directly reference any of the following standard Java classes, or classes derived
from them: Date, Boolean, Short, Integer, Long, Float, Double, Char, Byte, String, List, Array.
Calling nested objects
ColdFusion supports nested (scoped) object calls. For example, if an object method returns
another object and you must invoke a property or method on that object, you can use the
following syntax:
<cfset prop = myObj.X.Property>.
Similarly, you can use code such as the following CFScript line:
GetPageContext().include("hello.jsp?name=Bobby");
In this code, the ColdFusion
GetPageContext function returns a Java PageContext object, and
the line invokes the PageContext object’s
include method.
Creating and using a simple Java class
Java is a strongly typed language, unlike ColdFusion, which does not enforce data types. As a
result, there are some subtle considerations when calling Java methods. The following sections
create and use a Java class to show how to use Java effectively in ColdFusion pages.