Creating and using a simple Java class

Java is a strongly typed language, unlike ColdFusion, which does not enforce data types. As a result, some subtle considerations exist when calling Java methods.

The Employee class

The Employee class has four data members: FirstName and LastName are public, and Salary and JobGrade are private. The Employee class has three overloaded constructors and an overloaded SetJobGrade method.

Save the following Java source code in the file Employee.java, compile it, and place the resulting Employee.class file in a directory that is specified in the classpath:

public class Employee { 
 
public String FirstName; 
public String LastName; 
private float Salary; 
private int JobGrade; 
 
public Employee() { 
    FirstName =""; 
    LastName =""; 
    Salary = 0.0f; 
    JobGrade = 0; 
} 
 
public Employee(String First, String Last) { 
    FirstName = First; 
    LastName = Last; 
    Salary = 0.0f; 
    JobGrade = 0; 
} 
 
public Employee(String First, String Last, float salary, int grade) { 
    FirstName = First; 
    LastName = Last; 
    Salary = salary; 
    JobGrade = grade; 
} 
 
public void SetSalary(float Dollars) { 
    Salary = Dollars; 
} 
 
public float GetSalary() { 
    return Salary; 
} 
 
public void SetJobGrade(int grade) { 
    JobGrade = grade; 
} 
 
public void SetJobGrade(String Grade) { 
    if (Grade.equals("CEO")) { 
        JobGrade = 3; 
    } 
    else if (Grade.equals("MANAGER")) { 
        JobGrade = 2; 
    } 
    else if (Grade.equals("DEVELOPER")) { 
        JobGrade = 1; 
    } 
} 
 
public int GetJobGrade() { 
    return JobGrade; 
} 
 
}

A CFML page that uses the Employee class

Save the following text as JEmployee.cfm:

<html> 
<body> 
<cfobject action="create" type="java" class="Employee" name="emp"> 
<!--- <cfset emp.init()> ---> 
<cfset emp.firstname="john"> 
<cfset emp.lastname="doe"> 
<cfset firstname=emp.firstname> 
<cfset lastname=emp.lastname> 
</body> 
 
<cfoutput> 
    Employee name is #firstname# #lastname# 
</cfoutput> 
</html>

When you view the page in your browser, you get the following output:

Employee name is john doe

Reviewing the code

The following table describes the CFML code and its function:

Code

Description

<cfobject action=create type=java class=Employee name=emp>

Loads the Employee Java class and gives it an object name of emp.

<!--- <cfset emp.init()> --->

Does not call a constructor. ColdFusion calls the default constructor when it first uses the class; in this case, when it processes the next line.

<cfset emp.firstname="john"> 
<cfset emp.lastname="doe">

Sets the public fields in the emp object to your values.

<cfset firstname=emp.firstname> 
<cfset lastname=emp.lastname>

Gets the field values back from emp object.

<cfoutput>     
Employee name is #firstname# #lastname# 
</cfoutput>

Displays the retrieved values.

Java considerations

The following points are important when you write a ColdFusion page that uses a Java class object:

  • The Java class name is case sensitive. Ensure that the Java code and the CFML code use Employee as the class name.

  • Although Java method and field names are case sensitive, ColdFusion variables are not case sensitive, and ColdFusion does any necessary case conversions. As a result, the sample code works even though the CFML uses emp.firstname and emp.lastname; the Java source code uses FirstName and LastName for these fields.

  • If you do not call the constructor (or, as in this example, comment it out), ColdFusion automatically runs the default constructor when it first uses the class.

Using an alternate constructor

The following ColdFusion page explicitly calls one of the alternate constructors for the Employee object:

<html> 
<body> 
 
<cfobject action="create" type="java" class="Employee" name="emp"> 
<cfset emp.init("John", "Doe", 100000.00, 10)> 
<cfset firstname=emp.firstname> 
<cfset lastname=emp.lastname> 
<cfset salary=emp.GetSalary()> 
<cfset grade=emp.GetJobGrade()> 
 
<cfoutput> 
    Employee name is #firstname# #lastname#<br>  
    Employee salary #DollarFormat(Salary)#<br> 
    Employee Job Grade #grade# 
</cfoutput> 
 
</body> 
</html>

In this example, the constructor takes four arguments: the first two are strings, the third is a float, and the fourth is an integer.