Constructors
What Is a Constructor?
A constructor is a special block of code that runs automatically when an object is created with new. Its job is to initialize the new object's fields so it starts life in a valid state. A constructor looks like a method, but with two important differences: it has exactly the same name as the class, and it has no return type at all — not even void.
A basic constructor
public class Point {
int x;
int y;
// Constructor — same name as the class, no return type
Point(int x, int y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Point p = new Point(3, 4); // the constructor runs here
System.out.println(p.x + ", " + p.y);
}
}3, 4
The Default Constructor
If you don't write any constructor at all, the compiler automatically supplies a default, no-argument constructor for you. It takes no parameters and does nothing beyond the default field initialization (0, false, null, and so on).
Relying on the compiler-provided default constructor
public class Empty {
int value; // defaults to 0
}
public class Main {
public static void main(String[] args) {
Empty e = new Empty(); // works — the compiler generated this constructor
System.out.println(e.value);
}
}class Empty { Empty(int v) { ... } }
new Empty(); // compile error — no matching constructor
Constructor Overloading
Just like regular methods, a class can define multiple constructors as long as their parameter lists differ (in number, order, or type). This is called constructor overloading, and it lets callers create objects in whichever way is most convenient for them.
Multiple constructors for the same class
public class Rectangle {
double width;
double height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
Rectangle(double side) { // a square is a rectangle with equal sides
this.width = side;
this.height = side;
}
Rectangle() { // default size
this.width = 1;
this.height = 1;
}
double area() {
return width * height;
}
}Constructor Chaining with this(...)
When several constructors share setup logic, you can have one constructor call another constructor in the same class using this(...). This avoids duplicating initialization code, and the call to this(...) must be the very first statement in the constructor.
Chaining constructors with this(...)
public class Rectangle {
double width;
double height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
Rectangle(double side) {
this(side, side); // delegates to the two-argument constructor above
}
Rectangle() {
this(1, 1); // delegates to the two-argument constructor above
}
double area() {
return width * height;
}
public static void main(String[] args) {
Rectangle r1 = new Rectangle(4, 5);
Rectangle r2 = new Rectangle(3); // a 3x3 square
Rectangle r3 = new Rectangle(); // a 1x1 default
System.out.println(r1.area());
System.out.println(r2.area());
System.out.println(r3.area());
}
}20.0 9.0 1.0
A constructor shares the class's name exactly and never declares a return type
The compiler only provides a free no-argument constructor if you define no constructors yourself
Overload constructors to offer convenient ways of building an object
Use this(...) to funnel overloaded constructors through one piece of shared initialization logic