🎯 What you will learn
Lesson 08 showed you how to create objects by assigning fields after calling new. That works, but it's fragile — a caller can forget to set a required field and leave the object in an invalid state. Constructors solve this by forcing initialisation at creation time. By the end of this lesson you will understand:
- What a constructor is and how it differs from a regular method
- How to overload constructors to support multiple creation patterns
- How
this()chains constructors to avoid duplicating initialisation logic - How
this.fielddisambiguates fields from same-named parameters - How to return
thisfrom methods to enable fluent method chaining - Why
finalfields must be set in the constructor
this and this()?" appear in virtually every OOP round. This lesson arms you with precise, confident answers to both.
🔧 What is a constructor?
A constructor is a special method called automatically when you new an object. It initialises the object's fields. If you don't write one, Java provides a no-arg default constructor that sets everything to defaults (0, false, null). The constructor name must match the class name and has no return type — not even void.
NairaSwift is a Lagos-based cross-border remittance platform serving 800,000 customers across West Africa. Chidi, a backend engineer, needs to model loan applications with sensible defaults (14% rate, 12-month tenure when not specified) and build a step-by-step wire transfer pipeline where each stage returns the object itself for clean method chaining.
this() rule: this() must be the very first statement in the constructor body. You cannot put any other code before it — the compiler enforces this to guarantee the chained constructor runs before any other initialisation.
🔗 The this keyword and method chaining
this inside an instance method or constructor refers to the current object — the one on which the method was called. It serves two main purposes: (1) this.field = param disambiguates a field from a same-named parameter; (2) return this enables fluent method chaining where each step returns the same object.
🔒 Immutable objects — final fields
Marking a field final means it can be assigned exactly once — in the constructor. Afterwards, any attempt to change it is a compile error. Immutable objects are safe to share across threads without synchronisation and make bugs easier to track because state cannot change unexpectedly after creation.
📋 Quick reference — constructor rules
| Rule | Example |
|---|---|
| Name must match class | NsLoan(...) inside class NsLoan |
| No return type | void NsLoan() is a compile error |
this() must be first statement | Cannot have code before this(...) |
final fields must be set in constructor | Cannot be assigned later |
| Compiler adds default no-arg if none defined | Disappears once you write any constructor |
🎤 Interview Q&A
The compiler provides a default no-argument constructor that calls super() and initialises all fields to their type defaults (0, false, null). As soon as you write any constructor yourself, the compiler no longer provides the default — if you still need a no-arg constructor, you must write it explicitly.
this() have to be the first statement in a constructor?Before the object's own initialisation, the parent object must be fully initialised. The JVM requires the parent constructor (or chained constructor) to run before any other code executes in the current constructor. This guarantees that inherited fields are in a valid state before the child constructor body runs.
this and this()?this is a reference to the current object and is used to access instance fields/methods (this.field) or pass the current object to another method. this() is a constructor call — it invokes another constructor in the same class. They look similar but are used in completely different contexts.
final?Declaring final String id in a constructor parameter prevents accidentally reassigning the parameter inside the constructor body (e.g., id = "oops" would be a compile error). It signals intent: this parameter is read-only input to the constructor. It doesn't affect the field — only the local parameter binding.
this() vs super()?this() chains to another constructor in the same class — useful for providing defaults without duplicating logic. super() calls a constructor in the parent class — required to initialise the inherited part of the object. Both must be the first statement and cannot coexist in the same constructor. When neither is written explicitly, the compiler inserts super() automatically (which fails if the parent has no no-arg constructor).
📝 Quiz — 5 questions
this() be in a constructor?this.field = param inside a constructor — what does this refer to?obj.a().b().c()?Next: Inheritance and super →
⚠️ Common mistakes
void NsLoan(...) makes it a regular method, not a constructor. The class gets a default no-arg constructor and the custom one is silently ignored as a strangely-named method.this() not the first statementthis(...) is a compile error. this() must be the first line of the constructor body — the compiler enforces this to guarantee parent/chained initialisation runs first.this., the field stays at its default value. Always write this.field = param when names clash — never rely on the compiler to guess which you mean.this from a void methodvoid methods cannot return anything. To enable method chaining, change the return type to the class name and add return this; — only then can callers write obj.a().b().c().✅ Key takeaways
- Constructors initialise objects; their name matches the class and they have no return type.
- Overloading constructors with
this()chaining avoids duplicating initialisation logic. this.field = paramdisambiguates fields from same-named parameters.- Returning
thisfrom methods enables fluent method chaining. finalfields must be set in the constructor and cannot be changed afterwards.
From constructors to production Spring Boot
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →