Constructors in Java: Setting Up Objects the Right Way
Learn Java constructors from scratch — what they are, how to write one, the this keyword, the default constructor gotcha, and constructor overloading, all with clear examples.
In the last lesson we built a Car object and then set its fields one by one — car1.brand = "Honda", car1.color = "red", and so on. That works, but it's clumsy and easy to forget a field. Constructors fix this: they let you set up an object properly the moment it's born. They're one of the most-used features in Java, and they're simpler than they sound.
What is a constructor?
A constructor is a special method that runs automatically when you create an object with new. Its job is to initialise the object — give its fields their starting values. A constructor has two rules that make it easy to spot:
- Its name is exactly the same as the class name.
- It has no return type — not even
void.
Writing a constructor
Let's give our Car a constructor so we can set the brand and colour in one clean step:
public class Car { String brand; String color; // constructor — same name as the class, no return type Car(String b, String c) { brand = b; color = c; } void describe() { System.out.println(color + " " + brand); } }
Now creating a fully set-up car is a single line:
Car car1 = new Car("Honda", "red"); Car car2 = new Car("Tata", "blue"); car1.describe(); // red Honda car2.describe(); // blue Tata
The values in the parentheses ("Honda", "red") are passed straight to the constructor's parameters. Much cleaner — and it's impossible to forget to set a field, because the constructor demands them.
The this keyword
You'll very often see constructor parameters named the same as the fields. When that happens, how does Java know which is which? The this keyword means "this object's own field":
Car(String brand, String color) {
this.brand = brand; // this.brand = the field; brand = the parameter
this.color = color;
}
This is the standard, professional style you'll see everywhere. Read this.brand = brand as "set my own brand field to the brand that was passed in." (We'll dig deeper into this in a later lesson.)
The default constructor
Here's something that's been happening invisibly: if you don't write any constructor, Java quietly gives you a free empty one (the "default constructor"). That's why new Car() worked in the last lesson even though we never wrote a constructor.
Car(String b, String c) and then try new Car() with no arguments, you'll get a compile error. The fix is easy — just add an empty constructor too if you still need it (see below).Multiple constructors (overloading)
A class can have several constructors, as long as they take different parameters. Java picks the right one based on what you pass:
public class Car {
String brand;
Car() { // no-argument constructor
brand = "Unknown";
}
Car(String brand) { // one-argument constructor
this.brand = brand;
}
}
Car a = new Car(); // brand = "Unknown"
Car b = new Car("Honda"); // brand = "Honda"
This is called constructor overloading — it gives users of your class flexible ways to create objects.
Constructors were a small "aha" for me: my objects went from being created half-empty and error-prone to being fully valid from the very first line. When you can't create a Car without giving it a brand, a whole category of "oops, I forgot to set that" bugs simply disappears. That's good design working for you.
Key takeaways
- A constructor runs automatically on
newand sets up the object's fields. - It has the same name as the class and no return type.
- Use
this.field = fieldto tell a field apart from a same-named parameter. - Java gives a free default constructor only until you write your own; you can have multiple (overloading).
← Previous: Lesson 12 — Classes and Objects
Next: Lesson 14 — Encapsulation in Java →