← Back to Core Java Course | Chapter 7: OOP Core | Lesson 3 of 11

Java Constructors

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

java
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);
	}
}

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

java
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);
	}
}

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

java
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);
	}
}

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

java
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);
	}
}

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()

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.