1. The problem: half-built objects
Student s = new Student();s.name = "Riya";// forgot rollNo and marks: the object is incomplete but nothing stops usSetting fields one by one is long-winded and error-prone. A constructor lets us give all the required values at the moment of creation.
2. Writing a constructor
class Student { String name; int rollNo; double marks; // Constructor: same name as the class, NO return type Student(String n, int r, double m) { name = n; rollNo = r; marks = m; }}Student s1 = new Student("Rohit", 12, 88.5); // one line, complete objectA constructor:
-
has exactly the class's name,
-
has no return type (not even
void), -
runs automatically when
newcreates the object.
3. The default constructor
If you write no constructor, Java quietly adds an empty one: Student() { }. That's why new Student() worked in Session 14. As soon as you write any constructor, Java stops adding the default one, so new Student() becomes an error unless you write a no-argument constructor yourself.
4. this: the current object
Good parameter names match the field names, but then there's a clash:
Student(String name, int rollNo, double marks) { name = name; // assigns the parameter to itself! The field stays null}Inside the constructor, name means the parameter (the nearest one wins). To reach the field, use this:
Student(String name, int rollNo, double marks) { this.name = name; // field = parameter this.rollNo = rollNo; this.marks = marks;}this is a reference to the current object, the one being constructed or the one a method was called on. When you call s1.printDetails(), inside that method this refers to the same object as s1.
5. Overloaded constructors and chaining
Like methods (Session 9), constructors can be overloaded. Use this(...) to call another constructor of the same class and avoid repeating code:
class Student { String name; int rollNo; double marks; String city; Student(String name, int rollNo, double marks, String city) { this.name = name; this.rollNo = rollNo; this.marks = marks; this.city = city; } Student(String name, int rollNo) { this(name, rollNo, 0.0, "Unknown"); // must be the FIRST statement } void printDetails() { System.out.println(rollNo + ": " + name + ", " + city + " (" + marks + ")"); }}new Student("Riya", 12, 88.5, "Pune").printDetails();new Student("Kabir", 7).printDetails(); // 7: Kabir, Unknown (0.0)6. Validation in constructors
Constructors are the perfect place to refuse bad data:
Student(String name, int rollNo, double marks) { if (marks < 0 || marks > 100) { System.out.println("Invalid marks, setting to 0"); marks = 0; } this.name = name; this.rollNo = rollNo; this.marks = marks;}7. What happens during new
8. Returning this for chaining
A method can return this so calls can be chained, just like StringBuilder.append :
class Counter { int value; Counter add(int n) { value += n; return this; }}// new Counter().add(5).add(10).add(2).value → 17Common mistakes
-
Writing a return type on a constructor:
void Student(...)is a method named Student, not a constructor. Your constructor silently doesn't exist. -
Forgetting
this.when parameter and field names match. -
Writing a parameterised constructor and then calling
new Student()without having defined a no-argument one. -
Putting
this(...)anywhere other than the first line.
Interview Questions
What is a constructor? How is it different from a method?
A constructor initialises a new object. It has the same name as the class, no return type, and is invoked automatically by new. Methods have a return type (or void), any name, and are called explicitly.
When does Java provide a default constructor?
Only when the class declares no constructors at all. The compiler then adds a public (or matching the class's access) no-argument constructor.
What is constructor chaining?
Calling one constructor from another using this(...) within the same class, or super(...) for the parent class. The call must be the first statement.
Can a constructor be private?
Yes. It prevents other classes from creating objects directly; it's used in singletons and utility classes.
Practice
-
easy Add constructors to your
Bookclass: one with all fields, and one with only title and author (price defaults to 0, pages to 100). -
easy Create a
Pointclass with x and y, a constructor, and a methoddistanceTo(Point other). -
medium Create a
BankAccountclass: account number, holder name, balance. Constructors for "open with initial deposit" and "open with zero balance" (chained). Methodsdeposit,withdraw(refuse if insufficient),printStatement. -
medium Create a
Timeclass (hours, minutes, seconds) with a constructor that normalises values:new Time(1, 75, 130)should become 2:17:10.



Discussion