Java Notes
Learn Java constructors from scratch — what a constructor does, default vs parameterized, this() chaining, constructor overloading, and common mistakes beginners make.
When you write new Student(), Java does not just create an empty object. It immediately runs a special block of code to set up the object. That block is called a constructor.
A constructor runs automatically every time an object is created. Its job is to give the object its starting values so it is ready to use from the first line you call on it.
Constructor vs Method — The Differences
Students often confuse constructors with methods because they look similar. They are not the same.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
| Feature | Constructor | Method |
|---|---|---|
| Name | Exactly same as class | Any valid name |
| Return type | Nothing — not even void | Must declare a return type |
| Called | Automatically when new runs | Called manually by your code |
| Purpose | Set up initial state | Perform an operation |
Default Constructor
If you do not write any constructor, Java silently provides one. It takes no parameters and sets all fields to their default values (0, null, false).
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
Point p = new Point();— Java calls the invisible default constructor.p.xis 0,p.yis 0 — default forint.p.labelisnull— default for any object reference.
Default values by type:
| Type | Default |
|---|---|
| int, long, short, byte | 0 |
| float, double | 0.0 |
| boolean | false |
| char | \u0000 |
| String, any object | null |
Important rule: As soon as you write even one constructor yourself, Java stops providing the default. If you still need a no-argument constructor, you must write it yourself.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Parameterized Constructor
A constructor that accepts parameters lets you set field values at the moment of creation, instead of assigning them one by one after.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
public Student(String name, int rollNumber, double cgpa)— constructor header. No return type.this.name = name;—this.nameis the object's field.name(right side) is the parameter. Withoutthis, Java cannot tell them apart.new Student("Asha", 101, 8.7)— the three values are passed directly into the constructor parameters.- Each object is fully set up in one line — no separate assignment lines needed.
this Keyword in Constructor
When a parameter has the same name as a field, this.field refers to the object's own field. Without this, the parameter shadows the field.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Always use this.fieldName inside a constructor when parameters share the same name as fields. It is clear and prevents the shadowing bug.
Constructor Overloading
A class can have multiple constructors — as long as their parameter lists are different. Java picks the right one based on what you pass at new.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
- Three constructors — same name
Rectangle, different parameter counts. new Rectangle()→ Java matches the no-argument version.new Rectangle(5)→ Java matchesRectangle(int side).new Rectangle(4, 7)→ Java matchesRectangle(int width, int height).
Constructor Chaining with this()
One constructor can call another constructor in the same class using this(). This avoids repeating initialization code.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
this(name, price, quantity, "General")— calls the four-argument constructor from inside the three-argument one.this()must be the first statement in the constructor body. Java enforces this.- Each shorter constructor delegates to a more complete one — no repeated assignment code.
Constructor With Validation
A constructor can check values and reject invalid ones before setting them.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
If invalid data is passed, the constructor throws an exception and no object is created. This guarantees that every object that exists is in a valid state.
Common Mistake 1: Writing a Return Type
// WRONG — void makes this a method, not a constructor
public void Student(String name) {
this.name = name;
}
// CORRECT — no return type at all
public Student(String name) {
this.name = name;
}A constructor with a return type becomes an ordinary method with the same name as the class. It will never be called automatically. This is a silent bug — Java will not give an error, but your object will not be initialized.
Common Mistake 2: Calling this() Not as First Statement
// WRONG
public Product(String name, double price) {
System.out.println("Creating product..."); // statement before this()
this(name, price, 1); // COMPILE ERROR
}
// CORRECT
public Product(String name, double price) {
this(name, price, 1); // must be first
System.out.println("Creating product...");
}Common Mistake 3: Forgetting this. When Names Match
// WRONG — radius stays 0.0
public Circle(double radius) {
radius = radius; // parameter assigns to itself
}
// CORRECT
public Circle(double radius) {
this.radius = radius; // field = parameter
}Practice Task
Task: Create a class Movie with fields: title (String), year (int), rating (double). Write three constructors:
- No-argument — sets title to "Unknown", year to 0, rating to 0.0
- Two-argument
(String title, int year)— sets rating to 5.0 by default - Full three-argument constructor
Use this() chaining where possible. In main, create one object using each constructor and print all three fields.
Hint: Start with the full constructor. The two-argument one calls this(title, year, 5.0). The no-argument one calls this("Unknown", 0).
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: All three constructors ultimately run through the three-argument one. Try adding a System.out.println("Constructor called") inside the full constructor — you will see it printed three times, once for each object.
Continue the lesson path