Learn Java
import com.learnjava.oopfundamentals.Constructors;

Constructors in Java

@Concept

A constructor is a special method, sharing its class's exact name and having no return type at all, not even void, that runs automatically whenever an object is created with new — its job is to initialize that new object's fields to a sensible starting state. If a class defines no constructor at all, Java silently supplies a default one that takes no arguments and does nothing beyond the automatic field defaults; the moment a class defines any constructor of its own, that free default one disappears entirely.

Constructors can be overloaded just like ordinary methods, letting objects be created several different ways. And this(...), called as the very first line of one constructor, can delegate to another constructor in the same class to avoid repeating the same initialization logic.

@HowItWorks
1

Matching the class name

A constructor's name must be identical to its class's name, with no return type, not even void.

2

Running automatically

new ClassName(...) always calls a matching constructor, initializing the object's fields as part of creation.

3

The implicit default constructor

A class with no constructors of its own gets a free, no-argument, do-nothing one automatically.

4

That default disappears once any constructor is written

Defining even one constructor removes the automatically-supplied no-argument one.

5

Constructor overloading and this(...)

Multiple constructors can coexist with different parameter lists, and this(...) as a constructor's first line delegates to another constructor in the same class.

@Example
public class Point {
int x;
int y;
 
Point(int x, int y) {
this.x = x;
this.y = y;
}
 
Point() {
this(0, 0);
}
 
public static void main(String[] args) {
Point p1 = new Point(3, 4);
Point p2 = new Point();
System.out.println(p1.x + "," + p1.y);
System.out.println(p2.x + "," + p2.y);
}
}
Output
3,4
0,0
1new Point(3, 4) calls the two-argument constructor directly, setting x and y from its arguments.
2this.x = x; assigns the parameter's value into the object's own field, distinguishing the two thanks to this.
3new Point() calls the no-argument constructor instead, whose only line, this(0, 0);, delegates straight to the two-argument version.
4Both println calls confirm p1 got the explicit values while p2 got the delegated defaults of 0 and 0.
@UseCases
Guaranteeing every object starts in a valid, fully-initialized state as soon as it's created
Offering multiple convenient ways to construct an object, with overloaded constructors
Avoiding duplicated initialization logic across constructors using this(...)
Understanding why a class stops getting a free no-argument constructor once any constructor is written

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.