🎯 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.field disambiguates fields from same-named parameters
  • How to return this from methods to enable fluent method chaining
  • Why final fields must be set in the constructor
💼 Interview reality: "What happens if you don't write a constructor?" and "What is the difference between 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.

💸
Real-world scenario · NairaSwift
NairaSwift — Chidi's wire transfer engine (Lagos)

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.

javac ConstructorBasics.java && java ConstructorBasics
ConstructorBasics.javaLesson 09 · constructor overloading + this() chaining
stdout
=== NairaSwift — Loan Applications === CHI-001 principal: ₦ 500,000 rate: 12.5% 24mo EMI: ₦23,653.65 CHI-002 principal: ₦ 200,000 rate: 14.0% 12mo EMI: ₦17,957.42 CHI-003 principal: ₦ 750,000 rate: 11.0% 36mo EMI: ₦24,554.04

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.

javac ThisKeyword.java && java ThisKeyword
ThisKeyword.javaLesson 09 · this.field disambiguation + method chaining
stdout
=== NairaSwift — Wire Transfer Pipeline === [validate] TXN-8801 ₦150,000.00 [approve] TXN-8801 → APPROVED [process] ₦150,000.00 NG-4421 → NG-7731 Result: TXN-8801 [COMPLETED]

🔒 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.

javac ImmutableTransfer.java && java ImmutableTransfer
ImmutableTransfer.javaLesson 09 · final fields set in constructor
stdout
=== NairaSwift — Immutable Transfer Records === Transfer[TXN-9001 ₦250,000.00 NG-5501 → NG-6601] Transfer[TXN-9002 ₦125,000.00 NG-3301 → NG-4401] Total: ₦375,000.00 Fields are final — cannot be changed after construction.

📋 Quick reference — constructor rules

RuleExample
Name must match classNsLoan(...) inside class NsLoan
No return typevoid NsLoan() is a compile error
this() must be first statementCannot have code before this(...)
final fields must be set in constructorCannot be assigned later
Compiler adds default no-arg if none definedDisappears once you write any constructor

🎤 Interview Q&A

EasyWhat happens if you don't write any constructor for a class?
+

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.

EasyWhy does 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.

MediumWhat is the difference between 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.

MediumWhy would you make constructor parameters 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.

HardWhat is the difference between constructor chaining with 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

Question 1 of 5
What must this() be in a constructor?
Question 2 of 5
If you write one constructor with parameters, what happens to the default no-arg?
Question 3 of 5
this.field = param inside a constructor — what does this refer to?
Question 4 of 5
What return type must a method have to support chaining like obj.a().b().c()?
Question 5 of 5
Which fields cannot be changed after the constructor runs?
0
/ 5
Quiz complete!

Next: Inheritance and super →

⚠️ Common mistakes

Writing a return type on a constructor
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 statement
Putting any code before this(...) 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.
Shadowed field never set
If the constructor parameter and field share a name and you forget 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.
Returning this from a void method
void 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 = param disambiguates fields from same-named parameters.
  • Returning this from methods enables fluent method chaining.
  • final fields must be set in the constructor and cannot be changed afterwards.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →