Java Constructors
In this page:
The Default Constructor
A constructor initializes a newly created object's state, and if you don't write one yourself, Java silently generates a no-argument default constructor that does nothing beyond default-initializing fields (0, false, or null depending on type).
Example: The Default Constructor
class Dog {
String name = "Unnamed";
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog(); // uses Java's silently generated default constructor
System.out.println(dog.name);
}
}
Login to try C/C++/Java/PHP code in the editor
Parameterized Constructors
Parameterized constructors accept arguments so the caller can set an object's initial field values right at creation time, e.g. new Point(3, 4), rather than creating the object and then setting each field separately afterward.
Example: Parameterized Constructors
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static void main(String[] args) {
Point p = new Point(3, 4);
System.out.println(p.x + "," + p.y);
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor Overloading
Constructor overloading lets a class define several constructors with different parameter lists, giving callers multiple valid ways to create an object — for instance, one constructor that takes full details and another that fills in sensible defaults for the rest.
Example: Constructor Overloading
class Point {
int x, y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static void main(String[] args) {
Point p1 = new Point();
Point p2 = new Point(3, 4);
System.out.println(p1.x + "," + p2.x);
}
}
Login to try C/C++/Java/PHP code in the editor
The Copy Constructor
A copy constructor takes an existing object of the same class and uses its field values to initialize a new, independent object — useful when you want a genuine duplicate rather than two variables referencing the same underlying object.
Example: The Copy Constructor
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
Point(Point other) {
this.x = other.x;
this.y = other.y;
}
}
public class Main {
public static void main(String[] args) {
Point original = new Point(3, 4);
Point copy = new Point(original);
System.out.println(copy.x + "," + copy.y);
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor Chaining with this()
this(...) inside a constructor calls another constructor in the same class, letting you funnel multiple constructors through one central initialization path instead of duplicating setup logic. Java requires this call to be the very first statement in the constructor body.
Example: Constructor Chaining with this()
class Point {
int x, y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Main {
public static void main(String[] args) {
Point p = new Point();
System.out.println(p.x + "," + p.y);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: