Stop 04 · Classes & ObjectsTopics · 2/8

Constructors & the this Keyword

Part of the Java Roadmap

In short

Setting five fields one by one after new is slow and easy to forget. Constructors make sure every object is born complete and valid, and this lets an object talk about itself.

Constructors initialise new objects and share the class name with no return type. this refers to the current object, and this(...) calls another constructor in the same class.

The jaadu sits at a college admission desk stamping a fresh ID card and handing it to a smiling new student.

Think of it like this

Admission day at college

On admission day, you cannot just walk into class. The office fills in your name, roll number and branch, hands you an ID card, and only then are you a student. A constructor is that admission desk: it runs once, right when the object is created, and sets it up properly.

Watch it happen

What happens on new Account("Sneha", 5000)

Allocate
Defaults
this(...)
Assign fields
Ready

1/5Memory for a new Account is reserved on the heap.

The big ideas

What you need to know

Same name, no return type

public Account(String owner) is a constructor. Adding void would turn it into an ordinary method.

this means this object

this.owner = owner; separates the field from the parameter with the same name.

this(...) chaining

One constructor can call another with this(...) as its very first line, to avoid repeating code.

The free default

If you write no constructor, Java adds an empty one. Write any constructor and that free one disappears.

See it in code

Walk through the code

java
1public class Account {2    String owner;3    String ifsc;4    double balance;5 6    public Account(String owner, String ifsc, double balance) {7        if (balance < 0) {8            throw new IllegalArgumentException("Opening balance cannot be negative");9        }10        this.owner = owner;11        this.ifsc = ifsc;12        this.balance = balance;13    }14 15    public Account(String owner) {16        this(owner, "SBIN0000001", 0);17    }18 19    public static void main(String[] args) {20        Account a = new Account("Sneha", "HDFC0001234", 5000);21        Account b = new Account("Imran");22        System.out.println(a.owner + " " + a.balance);23        System.out.println(b.owner + " " + b.ifsc + " " + b.balance);24    }25}

1/4Constructor: same name as the class, no return type.

A full constructor that validates its input, and a shorter one that chains to it with this(...).

Save yourself the pain

Common mistakes

Oops

owner = owner; inside the constructor.

Do this instead

That assigns the parameter to itself and the field stays null. Write this.owner = owner;

Oops

Writing public void Account(String owner) and wondering why it never runs.

Do this instead

void makes it a regular method. Constructors have no return type at all.

Your mission

Book constructor

~15 min

Quick check

Where must this(...) appear inside a constructor?

Go deeper

  1. 1Write a default constructor and a parameterised constructor.
  2. 2Use this.name = name to resolve shadowed field names.
  3. 3Chain constructors with this(...) to avoid duplicate code.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge