top of page
Troy Web Consulting Logo

Currently Hiring! Visit our Careers Page

Writer's pictureTroy Web Consulting

Using Inner-Classed Enums With ColdFusion And Java

I recently had to call a method from Java that required an Enum type. In my case I did not have control over the java code and the Enums were stored inside a Class called Enums. It was not readily apparent to me on how to access these inner-classed Enums and there wasn't any specific documentation that I could find on the topic. I struggled a bit with trying to make a string work (the true end result), using JavaCast and trying to instantiate the class and Enum directly. Thankfully the final solution was actually easier than expected.


First we need to get the Enum from the Java Class. <cfset local.FULL = createObject("Java", "com.my.path.datatypes.Enums$RegistrationResultsFormat").FULL />


Pay close attention to "$RegistrationResultsFormat". This resolves to my Enum "public enum RegistrationResultsFormat { ... }".


If you perform a CFDUMP on the results of the createObject you will get a list of your possible options. In my case, RegistrationResultsFormat had FULL, COURSE and ACTIVITY. It is important to note that a CFDUMP of both the core createObject as well as the results of .FULL are exactly the same. Do not fret, you are still getting the desired end result and you may use it as is.


Next we need to pass the Enum value into that Java method. In my example it is the second argument. <cfset local.results = anotherJavaMethod( javaCast("string", arguments.id), local.FULL) />


In my actual use case, I stored this result in the variables scope of my component and got the value inside the init method.

bottom of page