Detailed explanation
A constructor is a special declaration used to initialise a newly created Java object. It has exactly the same name as its class and has no return type, not even void. In a normal expression such as new Point(3, 4), Java allocates memory for the object and invokes the matching constructor to establish its initial state. Constructors commonly assign fields, validate arguments, and ensure that an object starts in a usable condition. Unlike ordinary methods, constructors cannot be called on an existing object to initialise it again.
Constructor overloading means declaring multiple constructors in the same class with different parameter lists. The lists must differ in the number, types, or order of parameter types; changing parameter names alone does not create a new overload. The compiler chooses the applicable constructor using the arguments and Java's overload-resolution rules. This makes object creation convenient without requiring callers to supply every value. For example, a Point may support an origin-based default as well as explicitly supplied coordinates, while both creation paths maintain the same initialisation rules.
In the given example, Point() delegates to Point(int x, int y) through this(0, 0). Therefore, new Point() produces coordinates (0, 0), while new Point(3, 4) produces (3, 4). Inside the two-argument constructor, this.x refers to the object's field, whereas x refers to the parameter. Delegating with this(...) avoids duplicating assignment or validation logic. Constructor chains must not form a cycle. They also involve superclass construction: if no explicit constructor invocation is provided, Java implicitly invokes super(), which requires an accessible no-argument superclass constructor.
Before constructor logic assigns values, instance fields receive default values, such as zero for int and null for references. An important interview distinction is that Java supplies a default constructor only when the class declares no constructors at all. Once you declare Point(int x, int y), you must explicitly add Point() if you want no-argument creation. Constructors are not inherited or overridden, although they can be overloaded. Interviewers usually check whether you understand these distinctions, can trace constructor delegation, and can explain why centralising initialisation prevents inconsistent object state.
Key points to remember
- A constructor matches the class name and declares no return type.
- Overloaded constructors differ by parameter count, types, or order of types.
- Constructor selection happens at compile time through overload resolution.
- this(...) delegates to another constructor in the same class and reduces duplicated initialisation.
- Java provides a default constructor only if the class declares no constructors.
Common mistakes candidates make
- Writing void before the class name, which declares a method rather than a constructor.
- Assuming that a compiler-provided no-argument constructor remains available after declaring a parameterised constructor.
- Trying to overload constructors by changing only parameter names.
Likely follow-up questions
What is the difference between this(...) and super(...)?
this(...) invokes another constructor in the same class, while super(...) invokes a constructor in the direct superclass. In Java 21 and earlier, either explicit invocation must be the first statement; Java 25 permits a restricted prologue before it.
Can a constructor be private?
Yes, a private constructor restricts where objects can be created directly. It is commonly used with static factory methods or in utility classes that should not be instantiated.
What happens if two overloaded constructors match the same argument?
Java selects the most specific applicable constructor when one exists. For example, passing null to overloads accepting String and Integer causes a compile-time ambiguity because neither type is more specific than the other.
Interview tip: Define a constructor first, trace both Point creation paths, and finish by distinguishing an explicit no-argument constructor from a compiler-provided default constructor.
