Java Constructors
In this page:
class ClassName {
ClassName(dataType parameter) {
this.field = parameter;
}
}
ClassName object = new ClassName(argument);
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
// Define the class Point
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p
Point p = new Point(3, 4);
// Print a line to the console
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
// Define the class Point
class Point {
int x, y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p1
Point p1 = new Point();
// Create a new Point object and store it in p2
Point p2 = new Point(3, 4);
// Print a line to the console
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
// Define the class Point
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;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in original
Point original = new Point(3, 4);
// Create a new Point object and store it in copy
Point copy = new Point(original);
// Print a line to the console
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()
// Define the class Point
class Point {
int x, y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p
Point p = new Point();
// Print a line to the console
System.out.println(p.x + "," + p.y);
}
}
Login to try C/C++/Java/PHP code in the editor
- Adding a return type to a constructor, as in
public void Person(), which makes it a normal method. - Defining a parameterized constructor and then calling
new Person(), which fails because the default one is no longer created. - Calling
this()orsuper()not as the first statement, which is a compile error.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: