Constructors in Java
In this lesson: Types of constructors in Java — default, parameterized, overloading and this() — rules and examples with output. In Hindi & English.
What is a constructor
A constructor is a special method that runs automatically when you create an object with new. Its job is to set up the object's starting values. It has the same name as the class and no return type.
The default constructor
class Student {
String name;
Student() { // default constructor
name = "Unknown";
}
}
// use:
Student s = new Student();
System.out.println(s.name);
A no-argument constructor sets default values. If you write no constructor at all, Java provides an invisible empty one so new still works.
Parameterised constructor
class Student {
String name;
int marks;
Student(String n, int m) { // parameterised
name = n;
marks = m;
}
}
// use:
Student s = new Student("Aman", 75);
System.out.println(s.name + " " + s.marks);
A parameterised constructor takes values so you can set up an object with custom data at creation. This is cleaner than creating an object and setting each field afterwards.
Constructor overloading
class Box {
int w, h;
Box() { w = 1; h = 1; } // default size
Box(int side) { w = side; h = side; } // square
Box(int w, int h) { this.w = w; this.h = h; } // custom
}
Having several constructors with different parameters is overloading. Java picks the one matching your arguments — new Box(), new Box(5), or new Box(4, 6).
Calling this()
class Box {
int w, h;
Box() {
this(1, 1); // call the other constructor
}
Box(int w, int h) {
this.w = w; this.h = h;
}
}
this() calls another constructor in the same class, so one can reuse another's setup instead of repeating code. It must be the first line in the constructor.
Constructor vs method
| Point | Constructor | Method |
|---|---|---|
| Name | Same as class | Any name |
| Return type | None | Required (or void) |
| When it runs | Automatically on new | When you call it |
| Purpose | Set up the object | Do a task |
Common mistakes
- Adding a return type. A constructor has none — adding one makes it a method.
- Wrong name. A constructor must exactly match the class name.
- Losing the default. If you add any constructor, the free default one is gone — add it back if needed.
this()not first. Athis()call must be the first statement in the constructor.
Summary
- A constructor runs automatically when an object is created.
- It has the class's name and no return type.
- Default takes no arguments; parameterised takes values.
- Overloading provides several constructors with different parameters.
this()calls another constructor to reuse its setup.
Next, learn this and super, or reuse code with inheritance.
Frequently Asked Questions
What is a constructor in Java?
new. It sets up the object's starting values. It has the same name as the class and no return type.What is the difference between a default and a parameterised constructor?
new Student("Aman", 18).What is constructor overloading?
Why does a constructor have no return type?
What does this() do in a constructor?
this() calls another constructor in the same class. It lets one constructor reuse another's setup code, avoiding repetition. It must be the first statement in the constructor.