DevLearningTools

MODULE 7 · LESSON 04

Objects and Instances

Turning a CFC into a usable object — the new keyword vs createObject(), and the exact order ColdFusion runs code in during instantiation.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

A CFC file on disk is just a blueprint — nothing happens until it's instantiated. ColdFusion has two ways to do that, and they behave slightly differently.

Learning Objectives

After completing this lesson, you'll be able to:

  • Instantiate a CFC with the new keyword.
  • Instantiate a CFC with createObject(), and explain how it differs from new.
  • State the exact order ColdFusion runs code in when an object is created.

new — the Modern, Common Way

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

new automatically calls the CFC's init() function, passing along whatever arguments were given.

createObject() — the Older, More Manual Way

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

createObject() only builds the raw object — init() has to be called separately and explicitly, or the object's properties are left unset.

The Instantiation Order

When a CFC is instantiated, ColdFusion runs code in a fixed sequence: first, any code sitting directly in the component body outside of a function (the pseudo-constructor); then, if using new (or if init() is called manually), the init() function itself.

StepWhat runs
1The component's pseudo-constructor — property declarations and any top-level code
2init() — either automatically (new) or manually (createObject().init())

Common Beginner Mistakes

Using createObject() and forgetting to call .init() afterward

createObject("component", "User") alone only builds an empty shell — none of the constructor logic runs until .init(...) is called explicitly on the result.

Assuming new and createObject() behave identically

new automatically invokes init() with the arguments given; createObject() does not — it requires a separate, explicit .init() call.

Best Practices

  • Prefer new Component(...) for new code — it's shorter and less error-prone than createObject() plus a manual .init() call.
  • Reserve createObject() for cases that genuinely need it, like building a component name dynamically from a string at runtime.

Interview Questions

What's the practical difference between new User() and createObject("component", "User")?

new automatically calls init() with the given arguments. createObject() only creates the raw object — init() has to be called manually afterward, or the object's properties are never set.

What runs first when a CFC is instantiated: the pseudo-constructor or init()?

The pseudo-constructor — any code sitting directly in the component body outside a function — always runs first, before init() executes.

Summary

In this lesson, you covered instantiating a CFC with new and with createObject(), and the fixed order ColdFusion runs the pseudo-constructor and init() in.

What's Next?

The next lesson covers properties — declaring a CFC's data, and the accessors attribute that auto-generates getters and setters.