A constructor is the code that runs when an object is first created, typically used to set up its initial state. ColdFusion doesn't have a dedicated constructor keyword — by strong convention, a method named init() plays that role, and new automatically calls it.
Learning Objectives
After completing this lesson, you'll be able to:
- Write an init() method that sets up an object's initial state.
- Explain the pseudo-constructor and exactly when it runs relative to init().
- Require certain arguments at construction time so an object can never exist in an invalid state.
A Typical init()
component accessors="true" {
property name="owner" type="string";
property name="balance" type="numeric";
function init(required string owner, numeric startingBalance = 0) {
variables.owner = arguments.owner;
variables.balance = arguments.startingBalance;
return this;
}
}<cfcomponent accessors="true">
<cfproperty name="owner" type="string">
<cfproperty name="balance" type="numeric">
<cffunction name="init">
<cfargument name="owner" type="string" required="true">
<cfargument name="startingBalance" type="numeric" required="false" default="0">
<cfset variables.owner = arguments.owner>
<cfset variables.balance = arguments.startingBalance>
<cfreturn this>
</cffunction>
</cfcomponent>init() should always return this — without it, new Component() ends up with an empty result instead of the constructed object.
The Pseudo-Constructor
Any code written directly in the component body, outside of any function, is the pseudo-constructor — it runs automatically every time the component is instantiated, before init() runs. Property declarations are the most common thing living there, but plain statements are allowed too.
component {
property name="createdAt";
variables.createdAt = now(); // pseudo-constructor code — runs before init()
function init() {
writeOutput("init() running, createdAt is already set: " & variables.createdAt);
return this;
}
}<cfcomponent>
<cfproperty name="createdAt">
<cfset variables.createdAt = now()>
<!--- pseudo-constructor code — runs before init() --->
<cffunction name="init">
createdAt is already set: <cfoutput>#variables.createdAt#</cfoutput>
<cfreturn this>
</cffunction>
</cfcomponent>Instantiation order is always: 1) pseudo-constructor (top-level code), then 2) init() — whether triggered automatically by new or called manually after createObject().
Common Beginner Mistakes
Forgetting return this at the end of init()
Without it, new Component() returns whatever init()'s last expression happens to be (often nothing useful) instead of the constructed object.
Assuming ColdFusion has a dedicated constructor keyword
It doesn't — init() is purely a naming convention that new specifically looks for and calls automatically. A method with any other name won't be auto-invoked.
Best Practices
- Mark genuinely required construction data as required in init()'s arguments — it prevents an object from ever existing in a half-initialized state.
- Keep the pseudo-constructor to simple property setup — put real logic inside init() or another explicit method, not loose in the component body.
Interview Questions
What method name does new automatically call after instantiating a component?
init() — by convention, not by a language keyword.
What runs first: the pseudo-constructor or init()?
The pseudo-constructor — any top-level code in the component body — always runs first, before init() executes.
Summary
In this lesson, you wrote an init() constructor, and covered the pseudo-constructor that always runs before it during instantiation.
What's Next?
The next lesson covers access modifiers — public, private, package, and remote — and the difference between the variables and this scopes inside a component.