Types of Constructor in Java

A constructor in Java is a special type of a method which is used to initialize objects. In other words, a constructor is a block of code that initializes an object. When you create an object of a class, at least one constructor is invoked.

Unlike regular methods, constructors have the same name as the class and do not have a return type. They are invoked automatically when you create an object of class. Understanding constructors is fundamental to gain the basic knowledge of Java because they play an important role in object creation and class behavior.

In this tutorial, we will learn about the types of constructors in Java with basic to advanced examples.

Types of Constructor in Java

In Java programming language, there are three types of constructor which are as follows:

  • Default Constructor
  • No-Argument Constructor
  • Parameterized Constructor

Let’s understand each type of constructor with the help of examples.

Types of Constructor in Java: The Ultimate guide with examples.

Default Constructor in Java

A default constructor is a constructor in Java that has no parameters. When you do not define any constructor in Java program, Java compiler automatically creates in the program. However, you create a constructor with no parameters explicitly, Java does not define it.

Key Features of Default Constructor

There are mainly three key points for default constructor you should keep in mind. They are:

  • Default constructor initializes objects with default values, such as 0 for int, null for objects, etc.
  • Java provides it automatically if no other constructors are defined.
  • If you define any constructor, Java no longer provides the default one—you must define it manually if needed.

Basic Example – Java Provides Default Constructor

Let’s write a Java program in which we will not define any constructor in the program, but Java automatically creates a default one.

class Animal {
    int age;
    String type;

 // No constructor defined; Java creates a default one
}

public class Main {
public static void main(String[] args) {
    Animal a = new Animal(); // default constructor is used.
    System.out.println(a.age);
    System.out.println(a.type);
  }
}
Output:
      0
      null

In this example, we have not provided any constructor, so Java automatically provides one. Default values are assigned (0 for int, null for String).

Explicit Default Constructor

Let’s write a Java program in which we will explicitly define a constructor with no parameters.

class Car {
    int speed;

 // Explicit default constructor.
    Car() {
        speed = 100;
        System.out.println("Default constructor called");
    }
    void display() {
        System.out.println("Speed: " + speed);
    }
}
public class Main {
    public static void main(String[] args) {
        Car c = new Car();  // Calls default constructor.
        c.display();        
    }
}
Output:
      Speed: 100

In this example, we have defined a constructor with no parameters. This constructor initializes speed with a specific value 100.

Default Constructor Gets Removed When Custom One is Added

Let’s write a Java program in which we will define a constructor, Java removes the default constructor.

class Dog {
    int age;
 // Custom constructor
    Dog(int a) {
      age = a;
    }
}
public class Main {
public static void main(String[] args) {
  // Dog d = new Dog(); // Compilation error: no default constructor
     Dog d = new Dog(5);  // Correct.
     System.out.println(d.age);
  }
}

No-Argument Constructor

A no-argument constructor in Java is a constructor that does not take any parameters. It is not always the same as a default constructor. If you explicitly define it, it’s still a no-arg constructor but it is no longer default because it’s not provided by Java automatically.

Syntax to Define No-Argument Constructor

The general syntax to define no-argument constructor in Java is as follows:

class ClassName {
 // No-argument constructor
    ClassName() {
     // Initialization logic
    }
}

Basic Example of No-Argument Constructor

Let’s take an example in which we will define no-argument constructor to initialize data for instance variables.

class Person {
    String name;

 // No-argument constructor
    Person() {
       name = "John";
    }
    void display() {
       System.out.println("Name: " + name);
    }
}
public class Main {
public static void main(String[] args) {
    Person p = new Person(); // Calls the no-arg constructor
    p.display();            
  }
}
Output:
      Name: John

Parameterized Constructor in Java

A parameterized constructor in Java is a constructor that accepts arguments (or parameters). It allows you to initialize objects with custom values at the time of creation. The general syntax to define parameterized constructor in Java is as:

class ClassName {
 // Parameterized constructor
    ClassName(Type1 arg1, Type2 arg2, ...) {
        // Initialization code
    }
}

Basic Example of Parameterized Constructor

class Student {
    String name;
    int age;

 // Parameterized constructor
    Student(String n, int a) {
        name = n;
        age = a;
    }
    void display() {
        System.out.println(name + " - " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Amit", 20);
        s1.display();
    }
}
Output:
      Amit - 20

Constructor Overloading with Parameterized Constructors

Let’s an example in which we will perform constructor overloading in Java with parameterized constructor.

class Rectangle {
    int length, width;
 // No-arg constructor
    Rectangle() {
        length = 1;
        width = 1;
    }
 // Parameterized constructor
    Rectangle(int l, int w) {
        length = l;
        width = w;
    }
    int area() {
        return length * width;
    }
}
public class Main {
public static void main(String[] args) {
     Rectangle r1 = new Rectangle();       // Uses no-arg constructor
     Rectangle r2 = new Rectangle(5, 10);  // Uses parameterized

     System.out.println(r1.area());
     System.out.println(r2.area());
   }
}
Output:
      1
      50

If you’re looking to deepen your understanding of Java concepts like constructors, object-oriented programming, and more, be sure to visit Scientech Easy. This platform offers clear explanations, practical examples, and structured tutorials tailored for both beginners and advanced Java developers. Whether you’re preparing for interviews or building real-world projects, Scientech Easy provides the insights and resources you need to strengthen your Java skills effectively.

Please Share Your Love.

Leave a Reply

Your email address will not be published. Required fields are marked *