Constructors & Overloading
A constructor is a special method that runs automatically when you create an object with new. Its job is to put the object into a valid starting state — to assign its fields. A constructor has the same name as the class and no return type. Overloading lets you define several constructors (or methods) with the same name but different parameter lists, so callers can create or call them in whichever way suits them.
The default constructor
If you write no constructor at all, Java provides an invisible no-argument one that sets fields to their defaults.
public class Student {
String name; // defaults to null
int marks; // defaults to 0
}
// new Student() works because Java supplied a default constructor.
The moment you write any constructor yourself, this freebie disappears.
A parameterized constructor
A parameterized constructor takes arguments so you can set up the object in one step.
public class Student {
String name;
int marks;
// Constructor: same name as the class, no return type.
Student(String name, int marks) {
this.name = name; // 'this' distinguishes field from parameter
this.marks = marks;
}
}
// Usage:
Student a = new Student("Asha", 72); // fully initialised on creation
This is cleaner and safer than creating a blank object and setting fields one by one.
Constructor overloading
You can offer several constructors with different parameter lists. The compiler picks the matching one based on the arguments you pass.
public class Student {
String name;
int marks;
Student() { // 1) no-arg
this("Unknown", 0); // delegates to the two-arg version via this()
}
Student(String name) { // 2) name only
this(name, 0);
}
Student(String name, int marks) { // 3) full
this.name = name;
this.marks = marks;
}
}
The this(...) call lets one constructor reuse another, avoiding duplicated assignment code. It must be the first statement in the constructor.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesMethod overloading
The same idea applies to ordinary methods: same name, different parameter lists. Java resolves the call by the number and types of arguments.
public class MathTools {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
// add(2, 3) calls the first; add(2.5, 1.0) the second; add(1, 2, 3) the third.
Overloading is decided at compile time based on argument types — this is sometimes called "compile-time polymorphism".
What does NOT distinguish overloads
Overloads must differ in their parameter list (number, types, or order). They cannot differ only by return type — int total() and double total() clash and will not compile.
Common mistakes
- Giving a constructor a return type.
void Student()is not a constructor — it is a normal method that happens to share the class name, andnewwill not call it. - Expecting the default constructor after adding your own. Once you write any constructor, add a no-arg one explicitly if you still need it.
- Putting
this(...)after other statements. A constructor-to-constructor call must be the very first line. - Trying to overload by return type alone. Java needs a difference in parameters.
- Duplicating field assignments across constructors. Use
this(...)chaining to keep one source of truth.
FAQ
Can a constructor be private? Yes — for example, to force creation through a factory method or to enforce a single instance.
Is overloading the same as overriding? No. Overloading is multiple methods with the same name in one class (resolved at compile time). Overriding is a subclass redefining a parent's method (resolved at runtime) — covered in the polymorphism tutorial.
Keep going
Next, reuse code across classes with Inheritance in Java, or revisit Classes & Objects. All tutorials live on the Java hub; for guided practice in Jalgaon, see the Java course.
Write robust object setup code at Infoplanet, Jalgaon. Join the waitlist for the Java course.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in Java
Store many values of one type with Java arrays: declaring, initializing, indexing, iterating, multidimensional arrays, and the Arrays helper class.
Classes & Objects in Java
Define classes with fields and methods, create objects with new, use the this keyword, and understand instance vs static members in Java.
The Java Collections Framework
Use the Java Collections Framework — List, Set, and Map with ArrayList and HashMap — plus generics and iteration, to store flexible groups of objects.
