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

Java Constructors

A constructor is the setup step that runs when a new object is made, like filling in a name tag for a new student. It gives the object its first values.
Syntax
java
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

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

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

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

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
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Adding a return type to a constructor, as in public void Person(), which makes it a normal method.
  2. Defining a parameterized constructor and then calling new Person(), which fails because the default one is no longer created.
  3. Calling this() or super() not as the first statement, which is a compile error.

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.