Constructors & the this Keyword
Constructors & the this Keyword
In the previous lesson you gave a class fields and methods that describe state and behaviour. But how does an object come to life with meaningful data in the first place? That is the job of a constructor. And once you are inside a constructor, how do you refer to the object being built? That is the job of the this keyword.
What is a Constructor?
A constructor is a special method that Java calls automatically when you write new ClassName(...). It looks like a method but has no return type (not even void) and its name must match the class exactly, including capitalisation.
The Default Constructor
If you write no constructor at all, Java silently generates a default (no-argument) constructor for you. It initialises every field to its zero value: 0 for numbers, false for booleans, and null for object references.
This is fine for a quick prototype, but most real classes need their fields set to meaningful values right at creation time.
Parameterised Constructors
A parameterised constructor accepts arguments so callers can supply the initial state:
Now every BankAccount is guaranteed to start with a named owner and an explicit balance. The compiler will refuse to let you call new BankAccount() without arguments, which prevents accidentally creating accounts with null owners.
The this Keyword — Disambiguation
In the example above, the constructor parameter is named owner and so is the field. Inside the constructor, the local parameter shadows the field. Writing plain owner = owner would assign the parameter to itself — a no-op. this solves this cleanly:
this.owner— refers to the field on the current object.owner(no prefix) — refers to the local parameter.
this is simply a reference to the object currently being constructed (or on which a method is being called). It is available in any non-static method or constructor.
this) is the most common Java convention. It makes the intent crystal-clear: "assign the parameter to the same-named field."
Constructor Overloading
Java allows multiple constructors as long as their parameter lists differ (different count or types). This is called constructor overloading:
Constructor Chaining with this()
Duplicating initialisation logic across constructors is fragile — if the logic changes, you must update every copy. Constructor chaining solves this: one constructor calls another in the same class using this(...).
this(...) call must be the very first statement inside the constructor. You cannot do any other work before delegating. This guarantees the object is fully initialised before any additional logic runs.
The pattern — one fully-specified constructor doing all the work, shorter constructors delegating to it — keeps your initialisation logic in one place and is easy to maintain.
Summary
- A constructor initialises a new object. It has no return type and matches the class name.
- The default constructor is free when you write none; it vanishes once you write your own.
- Parameterised constructors enforce that required data is supplied at creation.
this.fielddistinguishes a field from a same-named parameter inside a constructor or method.this(...)chains one constructor to another in the same class and must be the first statement.