A constructor looks like a method, but Java calls it automatically when new creates an object. That similarity causes confusion about return types and defaults. Constructors differ from methods in how Java invokes them, and overloading, this(), super() and common errors follow from that distinction.
What a constructor does in Java
Recognise a constructor by its declaration: its name exactly matches its class name and it has no return type. A class-instance creation expression such as new Student(...) selects a constructor. Inside another constructor, this(...) selects one in the same class and super(...) selects one in the parent. Student() can be a constructor, but void Student() is an ordinary method.
Java allocates the object, its fields first hold type-default values, initialisation runs, and the selected constructor establishes the starting state. The constructor does not "return the object". Its declaration has no return type.
For Java foundations, use the Coding & Skills category.

Write and call a parameterised constructor
Save this complete program as Student.java:
public class Student {
int rollNo;
String name;
double cgpa;
Student(int rollNo, String name, double cgpa) {
this.rollNo = rollNo;
this.name = name;
this.cgpa = cgpa;
}
void printDetails() {
System.out.println(rollNo + " | " + name + " | " + cgpa);
}
public static void main(String[] args) {
Student first = new Student(42, "Asha", 8.6);
Student second = new Student(17, "Kabir", 7.9);
first.printDetails();
second.printDetails();
}
}The first call maps 42, "Asha" and 8.6 to the parameters. this.rollNo selects the current object's field, while bare rollNo selects the parameter. The second call initialises another object, not the class or first instance.
42 | Asha | 8.6
17 | Kabir | 7.9Default, no-argument and copy-style constructors
If a class declares no constructor, the compiler provides a default no-argument constructor. This complete program prints 0 because int fields begin at zero:
public class Counter {
int value;
public static void main(String[] args) {
System.out.println(new Counter().value);
}
}Once you declare any constructor, Java does not add that default. If the class has only Counter(int start), new Counter() is a compile-time error. Add the zero-parameter constructor explicitly:
public class Counter {
int value;
Counter() { value = 10; }
Counter(int start) { value = start; }
public static void main(String[] args) {
System.out.println(new Counter().value); // 10
System.out.println(new Counter(25).value); // 25
}
}Counter() is a no-argument constructor, though people informally call it a default constructor.
Java has no special copy-constructor syntax, but a constructor may accept another instance:
public class Rectangle {
int width, height;
Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
Rectangle(Rectangle other) {
this(other.width, other.height);
}
public static void main(String[] args) {
Rectangle original = new Rectangle(5, 3);
Rectangle copy = new Rectangle(original);
copy.width = 8;
System.out.println("original: " + original.width + " x " + original.height);
System.out.println("copy: " + copy.width + " x " + copy.height);
}
}The outputs are original: 5 x 3 and copy: 8 x 3. The primitive fields are independent, but this does not promise a deep copy of arrays or objects.
Constructor overloading and chaining with this()
Overloads provide several ways to create a Box. this(...) selects another constructor in the same class. The example uses the Java 21-compatible form with this(...) first; Java 25 also permits a restricted prologue before it.
public class Box {
int length, width, height;
Box() { this(1, 1, 1); }
Box(int side) { this(side, side, side); }
Box(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
}
int volume() { return length * width * height; }
public static void main(String[] args) {
System.out.println("unit box volume = " + new Box().volume());
System.out.println("cube volume = " + new Box(4).volume());
System.out.println("cuboid volume = " + new Box(2, 3, 5).volume());
}
}The calculations are 1 * 1 * 1 = 1, 4 * 4 * 4 = 64, and 2 * 3 * 5 = 30. The exact output is:
unit box volume = 1
cube volume = 64
cuboid volume = 30Every path reaches one assignment point, avoiding inconsistent initialisation blocks. A cycle such as Box() -> Box(1) -> Box() fails at compile time.

Parent construction and super()
A child constructor must eventually invoke a parent constructor. In this program, super(name) runs before the assignment to the employee's field:
public class EmployeeDemo {
static class Person {
String name;
Person(String name) {
this.name = name;
System.out.println("Person constructor: " + name);
}
}
static class Employee extends Person {
int employeeId;
Employee(String name, int employeeId) {
super(name);
this.employeeId = employeeId;
System.out.println("Employee constructor: " + employeeId);
}
}
public static void main(String[] args) {
new Employee("Meera", 105);
}
}It prints Person constructor: Meera before Employee constructor: 105. super(...) selects a parent constructor. If omitted, Java tries an implicit super(), which fails here because Person has Person(String), not Person(). Constructors are neither inherited nor overridden.
Starting with Java 25, Flexible Constructor Bodies permits a restricted prologue before this(...) or super(...). The prologue may validate arguments and assign certain uninitialised fields, but it cannot use this, invoke instance methods or access inherited state. The example keeps super(name) first, so it also compiles on older releases.
Common constructor errors and how to fix them
Common failures have exact fixes:
Faulty code | Problem | Correction |
|---|---|---|
| Declares a method | Remove |
| Assigns the parameter to itself | Use |
| No matching constructor | Call |
Code compiled for Java 21 or earlier must place this(...) or super(...) first. Java 25 permits a restricted prologue, so a statement such as System.out.println(...) may precede the invocation because it does not use the object under construction. One constructor still cannot invoke both this(...) and super(...) directly; follow a single chain to the parent constructor.
Keep unrelated heavy work out of constructors. Establish a valid state, then use a named method or factory for extra work. Copy arrays or other mutable objects explicitly when copy-style instances must be independent.
How coding assessments and interviews test constructors
Representative practice asks you to trace Box for volumes 1, 64 and 30; spot the missing super(name) when Person() does not exist; or correct id = id. Practise each as an output trace, a compiler diagnosis and a short correction.
Try a five-minute extension: add Student(int rollNo, String name) { this(rollNo, name, 0.0); }. Then new Student(31, "Neha") prints 31 | Neha | 0.0. Next, reject width <= 0 or height <= 0 in Rectangle. Accept new Rectangle(5, 3), but make new Rectangle(0, 3) throw IllegalArgumentException.
To see constructors inside node-based code, study Binary Trees and Binary Search Trees. Then use Sorting Algorithms: Complexity and Comparison and Dynamic Programming Explained: 0/1 Knapsack as subsequent DSA practice. Those articles practise data structures and algorithms, not constructor syntax.
Constructors in Java: the short version and next step
A constructor has the class name and no return type.
A compiler-provided default exists only when the class declares no constructor.
Overloads offer different valid entry points for object creation.
this()andsuper()select constructor paths. Java 21 and earlier require the invocation first; Java 25 permits a restricted prologue.
Run the Student and Box programs. Change one argument at a time, then trigger the missing-no-argument-constructor error. Continue with the Java course for structured language study, followed by the DSA using Java course for the next placement-focused step.




