🐣
OOP

Constructor

An Object's Birth
💡 A constructor is a "welcome kit" that gives an object its starting values the moment it's born.

A constructor is a special method with the same name as the class, called automatically when an object is created with the 'new' keyword.

It lets us give an object a valid starting state right away — without setting each field separately.

class Student {
  String name;
  Student(String n) { name = n; }
}
Student s1 = new Student("Aarav");
🐣
A constructor is a "welcome kit" that gives an object its starting values the moment it's born.
1 / 6
⚡ Quick Recap
  • A special method named like the class
  • Auto-called the moment 'new' runs
  • Sets initial values
On this page (4 subtopics)

A default constructor takes no arguments and sets fields to their default values (0, null, false) — if you don't write anything yourself, Java automatically provides a "no-arg" constructor (technically, this is what's called the "default constructor").

A parameterized constructor takes arguments so an object can be given meaningful starting values right away — in real projects, most classes use a parameterized constructor, since an "empty" object is rarely useful.

Important: if you write any constructor yourself (parameterized or no-arg), Java stops giving you the automatic default constructor — if you want both (no-arg and parameterized), you'll have to write both explicitly.

class Student {
  String name;
  Student() { name = "Unknown"; }             // no-arg
  Student(String n) { name = n; }             // parameterized
}
⚠️Common Mistake: If the Student class only has Student(String n), then new Student() (without an argument) will give a compile error — "no default constructor available". Java only gives you the automatic default constructor when YOU write no constructor at all.

A class can have multiple constructors, with different parameters (number or type) — like Java's built-in classes do (new ArrayList() or new ArrayList(20) or new ArrayList(anotherList) are all valid). This gives multiple flexible ways to build an object, and the caller can choose based on their situation.

class Rectangle {
  int width, height;
  Rectangle() { this(1, 1); }                 // default: 1x1
  Rectangle(int side) { this(side, side); }    // square
  Rectangle(int w, int h) { width = w; height = h; }
}

this(...) lets one constructor call another constructor (of the same class) — to avoid duplicating initialization code. Look at the Rectangle example above — see how each smaller constructor calls the bigger one.

super(...) lets a child class's constructor call the parent class's constructor — so the parent's setup happens first.

Both have a strict rule: this call must be the *very first* line of the constructor — you can't write both together (this() and super() can't be in the same constructor), and no other code can come before them.

class Student {
  String name; int age;
  Student(String name) { this(name, 18); }        // chaining
  Student(String name, int age) { this.name = name; this.age = age; }
}

A constructor is tied to the class's name, so a child class does NOT directly inherit the parent's constructor — this is a common point of confusion. But the child can call the parent's constructor via super(...) inside its own constructor, and if the child writes nothing, Java automatically calls super() (the parent's no-arg constructor) as default behavior.

⚠️Common Mistake: If the parent class has only a parameterized constructor (no no-arg constructor), and the child class doesn't write anything explicitly, you'll get a compile error — because Java will try to implicitly call super(), which doesn't exist.