Why Constructors Exist
On the previous page you built classes and created objects with new. A constructor is the code that actually runs during that new call. Its one purpose is to hand back an object that is ready to use - all its required fields set, no half-initialized state.
A constructor looks like a method, with two differences: it has the same name as the class, and it has no return type - not even void.
new Point(3, 4) allocates the object and then runs the constructor body with x = 3 and y = 4. By the time new returns, p is fully initialized.
The this Keyword
Inside the constructor above, the parameters are named x and y - the same as the fields. this.x means "the field x belonging to this object," while plain x refers to the parameter. Without this, x = x would just assign the parameter to itself and leave the field untouched.
You only need this when a parameter shadows a field, but many people use it everywhere for clarity. The common mistake is to forget it when names collide - the code compiles, runs, and silently leaves your fields at their defaults (null, 0, false).
The Default Constructor
If you never write a constructor, Java quietly supplies a default constructor: a public, no-argument constructor that does nothing extra. That is why new worked on classes that had no constructor at all.
The catch: as soon as you write any constructor, the free one is gone.
class Box {
int size;
Box(int size) { // now there is NO no-arg constructor
this.size = size;
}
}
new Box(); // compile error: no constructor Box() exists
If you still want new Box() to work, declare the no-arg constructor yourself:
Constructor Overloading
A class can have several constructors as long as their parameter lists differ - this is just method overloading applied to constructors. Each one offers a different way to build the object.
Java picks the matching constructor by the number and types of arguments you pass to new.
Chaining with this()
Notice the duplication above: each constructor assigns fields itself. You can avoid that by having one constructor call another with this(...). The call must be the first statement in the constructor.
Now the actual initialization lives in one place. The smaller constructors just fill in defaults and forward the work. If you ever try to put a statement before this(...), the compiler rejects it.
Constructors and super()
Every constructor implicitly calls its superclass constructor first. If you write nothing, Java inserts a hidden super() (the no-arg superclass constructor) at the top of the body. You will work with this directly once you start subclassing - which is the next topic.
class Animal {
String name;
Animal(String name) { this.name = name; }
}
class Dog extends Animal {
Dog(String name) {
super(name); // must call the parent constructor explicitly here
}
}
Because Animal has no no-arg constructor, Dog must call super(name) explicitly - there is no free super() to fall back on. Like this(), a super(...) call has to be the first statement in the constructor.
Next: Inheritance
Constructors initialize a single object, but super() already hinted at something bigger: classes can build on other classes, reusing their fields, methods, and constructors. That relationship - one class extending another - is inheritance, and it is the next page.
Frequently Asked Questions
What is a constructor in Java?
A constructor is a special method that runs when you create an object with new. It has the same name as the class and no return type (not even void). Its job is to put the new object into a valid starting state - usually by assigning the constructor's arguments to the object's fields.
What is the difference between a constructor and a method in Java?
A constructor has the exact same name as the class and declares no return type, and it can only be invoked with new when an object is created. A regular method has its own name, declares a return type (or void), and is called on an existing object. Constructors initialize; methods do work afterward.
What happens if I do not write a constructor in Java?
The compiler gives you a free no-argument default constructor that takes no parameters and does nothing beyond the implicit super() call. But the moment you write any constructor yourself, that free one disappears - so if you add a parameterized constructor and still want new Thing() to work, you must declare the no-arg constructor explicitly.