⏱️ 8 min read • Beginner Level • Lesson 48
A constructor in Java is a special block of code that is called automatically when an object is created. It is mainly used to initialize the instance variables of an object.
Before learning constructors, you should understand Java class and objects, Java class members, and Java methods.
A constructor is a special member of a class that initializes an object.
It is automatically invoked when an object is created using the new keyword.
void.public, private, and protected.class ClassName {
ClassName() {
// initialization code
}
}
Java mainly has two commonly discussed types of constructors:
| Constructor Type | Meaning |
|---|---|
| Default Constructor | A constructor with no parameters. |
| Parameterized Constructor | A constructor with one or more parameters. |
A default constructor is a constructor that does not take any parameter. It can be written by the programmer, or Java compiler can provide it automatically when no constructor is defined.
class DefaultConstructorExample {
DefaultConstructorExample() {
System.out.println("Default Constructor Called");
}
public static void main(String[] args) {
DefaultConstructorExample obj = new DefaultConstructorExample();
}
}
Output:
Default Constructor Called
If you do not write any constructor in a class, Java compiler automatically creates a default constructor for that class.
class Employee {
int id;
String name;
void showDetails() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
public static void main(String[] args) {
Employee e = new Employee();
e.showDetails();
}
}
Output:
ID: 0 Name: null
0 for int
and null for String.
A parameterized constructor accepts values as parameters and uses them to initialize object data. It is useful when each object should have different values.
class Employee {
int id;
String name;
Employee(int employeeId, String employeeName) {
id = employeeId;
name = employeeName;
}
void showDetails() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
public static void main(String[] args) {
Employee e1 = new Employee(123, "Ayan");
Employee e2 = new Employee(234, "Atif");
e1.showDetails();
e2.showDetails();
}
}
Output:
ID: 123 Name: Ayan ID: 234 Name: Atif
Constructor overloading means creating multiple constructors in the same class with different parameter lists.
class ConstructorDemo {
ConstructorDemo() {
System.out.println("Default Constructor");
}
ConstructorDemo(int number) {
System.out.println("Parameterized Constructor: " + number);
}
ConstructorDemo(String text) {
System.out.println("Parameterized Constructor: " + text);
}
public static void main(String[] args) {
ConstructorDemo obj1 = new ConstructorDemo();
ConstructorDemo obj2 = new ConstructorDemo(10);
ConstructorDemo obj3 = new ConstructorDemo("Java");
}
}
Output:
Default Constructor Parameterized Constructor: 10 Parameterized Constructor: Java
Constructor overloading works based on the parameter list.
When an object is created, Java checks the arguments passed inside new ClassName(...)
and selects the matching constructor.
Java can differentiate overloaded constructors by:
class Product {
Product() {
System.out.println("No-argument constructor");
}
Product(int id) {
System.out.println("Constructor with int parameter");
}
Product(String name) {
System.out.println("Constructor with String parameter");
}
Product(int id, String name) {
System.out.println("Constructor with int and String parameters");
}
Product(String name, int id) {
System.out.println("Constructor with String and int parameters");
}
public static void main(String[] args) {
Product p1 = new Product();
Product p2 = new Product(101);
Product p3 = new Product("Laptop");
Product p4 = new Product(101, "Mobile");
Product p5 = new Product("Keyboard", 202);
}
}
Output:
No-argument constructor Constructor with int parameter Constructor with String parameter Constructor with int and String parameters Constructor with String and int parameters
new Product() calls the no-argument constructor.new Product(101) calls the constructor with int parameter.new Product("Laptop") calls the constructor with String parameter.new Product(101, "Mobile") calls the constructor with int, String parameters.new Product("Keyboard", 202) calls the constructor with String, int parameters.
The this keyword refers to the current object. It is commonly used in constructors
when instance variable names and parameter names are the same.
this helps Java distinguish between them.
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
void showDetails() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
}
public static void main(String[] args) {
Student s1 = new Student(101, "Ayan");
s1.showDetails();
}
}
Output:
ID: 101 Name: Ayan
A constructor can be declared as private. When a constructor is private,
objects cannot be created from outside the class.
class Utility {
private Utility() {
System.out.println("Private constructor");
}
static void showMessage() {
System.out.println("Utility method called");
}
public static void main(String[] args) {
Utility.showMessage();
}
}
Output:
Utility method called
Constructors in Java can use access modifiers such as public,
protected, default, and private. These modifiers control
where objects of a class can be created from.
| Access Modifier | Where constructor can be accessed |
|---|---|
public |
From anywhere. |
protected |
Within same package and subclasses. |
| default | Only within the same package. |
private |
Only within the same class. |
class ConstructorAccessExample {
public ConstructorAccessExample() {
System.out.println("Public constructor");
}
protected ConstructorAccessExample(int id) {
System.out.println("Protected constructor");
}
ConstructorAccessExample(String name) {
System.out.println("Default constructor");
}
private ConstructorAccessExample(double salary) {
System.out.println("Private constructor");
}
public static void main(String[] args) {
ConstructorAccessExample obj1 = new ConstructorAccessExample();
ConstructorAccessExample obj2 = new ConstructorAccessExample(101);
ConstructorAccessExample obj3 = new ConstructorAccessExample("Ayan");
ConstructorAccessExample obj4 = new ConstructorAccessExample(50000.50);
}
}
Output:
Public constructor Protected constructor Default constructor Private constructor
private constructor cannot be called from another class directly.
It is commonly used in utility classes and singleton design patterns.
Constructor chaining means calling one constructor from another constructor. It helps avoid duplicate initialization code.
Constructor chaining can happen in two ways:
this().super().this() or super() must be the first statement inside a constructor.
In this example, one constructor calls another constructor of the same class using this().
class Student {
int id;
String name;
String course;
Student() {
this(101, "Ayan");
System.out.println("Default constructor called");
}
Student(int id, String name) {
this(id, name, "Java");
System.out.println("Two-parameter constructor called");
}
Student(int id, String name, String course) {
this.id = id;
this.name = name;
this.course = course;
System.out.println("Three-parameter constructor called");
}
void showDetails() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Course: " + course);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.showDetails();
}
}
Output:
Three-parameter constructor called Two-parameter constructor called Default constructor called ID: 101 Name: Ayan Course: Java
new Student().this(101, "Ayan").this(id, name, "Java").
In inheritance, the child class constructor can call the parent class constructor using
super().
class Person {
Person(String name) {
System.out.println("Person constructor called");
System.out.println("Name: " + name);
}
}
class Employee extends Person {
Employee(String name, int id) {
super(name);
System.out.println("Employee constructor called");
System.out.println("ID: " + id);
}
public static void main(String[] args) {
Employee e1 = new Employee("Ayan", 101);
}
}
Output:
Person constructor called Name: Ayan Employee constructor called ID: 101
Employee extends Person.Employee object is created, the child constructor starts.super(name) calls the parent class constructor.this() and super() together in the same constructor,
because both must be the first statement.
new.| Constructor | Method |
|---|---|
| Used to initialize an object. | Used to define behavior or perform operations. |
| Name must be same as class name. | Name can be any valid identifier. |
| Has no return type. | Must have a return type such as void, int, etc. |
| Called automatically when object is created. | Called manually using object or class name. |
Can a constructor be declared as static?
Answer: No. Constructors belong to object creation, while static members belong to the class itself.
public, protected, default, and private.this() is used to call another constructor in the same class.super() is used to call a parent class constructor.🧠 Test your understanding with a quick quiz
Topic: Constructors | Language: Java
class Demo {
Demo() {
System.out.println("Default Constructor");
}
public static void main(String[] args) {
Demo d = new Demo();
}
}class Product {
Product() {
System.out.println("No argument");
}
Product(int id) {
System.out.println("int argument");
}
public static void main(String[] args) {
Product p = new Product(101);
}
}🎉 Great job! Continue learning Java OOP step by step.