Design Patterns · 8 min read · Beginner
Static Members & Factory Methods
Use static helper methods and named factory constructors to create validated objects with expressive intent.
What you'll learn
- Distinguish instance members from class-level static members
- Replace confusing multi-argument constructors with named static factory methods
- Share read-only constants cleanly at the class level
A static method belongs to the class itself rather than a specific instance (Money.fromCents(1999) instead of new Money(19.99)). Static factory methods give object creation clear, self-documenting names.
Fill-in-the-Blank Exercise
Static Factory Method
Create a static factory fromCents() that converts cents into a Money instance.
class Money {
constructor(dollars) { this.dollars = dollars; }
fromCents(cents) {
return Money(cents / 100);
}
}Live OOP Playground
Named Static Factories
Live Result
Console Output
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
Score: 0 / 1
1. How is a static method called on a class named Duration?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.