1. Introduction
A constructor is a special block of code used to initialize an object at the moment it is created. Constructors have the same name as the class and no return type — not even void. Java automatically calls the appropriate constructor whenever an object is instantiated with the new keyword.
Cricket analogy: The toss ritual that always happens the moment a match begins, named after the match itself and producing no separate outcome document, is like a constructor—automatically invoked the instant an object is created with new.
Constructors let you guarantee that an object starts life in a valid, fully initialized state, avoiding the need to set every field manually after creation.
Cricket analogy: A player arriving at the crease already padded up, helmeted, and with a bat in hand guarantees a ready state, avoiding the chaos of equipping gear mid-over, just as a constructor guarantees a fully initialized object.
2. Syntax
class ClassName {
// default (no-arg) constructor
ClassName() {
// initialization code
}
// parameterized constructor
ClassName(dataType param1, dataType param2) {
// initialization using param1, param2
}
}3. Explanation
If a class defines no constructor at all, the Java compiler automatically inserts a public no-argument default constructor that initializes fields to their default values (0, null, false, etc.). However, the moment you define any constructor yourself — parameterized or not — the compiler no longer auto-generates the default one.
Cricket analogy: If a newly formed local cricket club never appoints an official captain, the league automatically assigns a default placeholder captain with no specific tactics; but the moment the club appoints even one real captain, that automatic placeholder is no longer provided.
Constructor overloading allows a class to have multiple constructors with different parameter lists, giving callers flexibility in how they initialize an object. Constructor chaining, using this(...), lets one constructor call another constructor of the same class to avoid duplicating initialization logic. The this(...) call, if used, must be the very first statement in the constructor.
Cricket analogy: A batting academy offers overloaded training packages—one for beginners needing full basics, another for advanced players needing only fine-tuning—mirroring constructor overloading; and just as an advanced package must first complete the beginner drills before adding extras, a this(...) call must be the very first statement.
Exam trap: If you write even one parameterized constructor and no no-arg constructor, calling new ClassName() will cause a compile-time error, because the compiler does NOT add a default constructor once you've defined any constructor.
Rule: this(...) used for constructor chaining must appear as the first executable statement in the constructor body; otherwise, it's a compile error.
4. Example
public class Box {
int length, width, height;
// no-arg constructor chains to parameterized constructor
Box() {
this(1, 1, 1);
System.out.println("No-arg constructor called");
}
// parameterized (overloaded) constructor
Box(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
System.out.println("Parameterized constructor called");
}
int volume() {
return length * width * height;
}
public static void main(String[] args) {
Box b1 = new Box();
System.out.println("Volume of b1: " + b1.volume());
Box b2 = new Box(2, 3, 4);
System.out.println("Volume of b2: " + b2.volume());
}
}5. Output
Parameterized constructor called
No-arg constructor called
Volume of b1: 1
Parameterized constructor called
Volume of b2: 246. Key Takeaways
- Constructors share the class name and have no return type.
- A default no-arg constructor is auto-generated only if no other constructor is defined.
- Constructor overloading provides multiple ways to initialize an object.
- Constructor chaining with this(...) must be the first statement in the constructor.
- Constructors run automatically whenever an object is created with
new.
Practice what you learned
1. What is the return type of a constructor in Java?
2. When does Java NOT auto-generate a default no-arg constructor?
3. Where must a this(...) call appear within a constructor?
4. What is constructor overloading?
5. If class Box has only Box(int, int, int), what happens when calling new Box()?
Was this page helpful?
You May Also Like
Classes and Objects in Java
Learn how classes act as blueprints and objects as instances in Java, with syntax, examples, and exam-focused explanations.
this Keyword in Java
Master the `this` keyword in Java — resolving field/parameter name conflicts, constructor chaining, and passing the current object.
static Keyword in Java
Learn how the static keyword creates class-level fields, methods, and blocks shared across all objects in Java.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics