DEV Community

Cover image for Constructors in Java with Default Constructor Explained
Vinayagam
Vinayagam

Posted on

Constructors in Java with Default Constructor Explained

Introduction

In Java, constructors play a crucial role in initializing objects. Whenever an object is created, a constructor is automatically invoked to assign initial values and set up the object properly. Understanding constructors is essential for writing clean and efficient object-oriented programs.

This article explains constructors in Java, focusing especially on the default constructor, its behavior, and how it differs from user-defined constructors.


What is a Constructor?

A constructor in Java is a special method used to initialize objects. It has the same name as the class and does not have a return type, not even void.

Key Characteristics:

  • Same name as the class
  • No return type
  • Automatically called when an object is created
  • Used to initialize instance variables

Example:

class Student {
    String name;
    int age;

    // Constructor
    Student() {
        name = "Unknown";
        age = 0;
    }

    void display() {
        System.out.println(name + " " + age);
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Types of Constructors in Java

1. Default Constructor

A default constructor is a constructor that is automatically provided by the Java compiler if no constructor is defined in the class.

It assigns default values:

  • int → 0
  • double → 0.0
  • boolean → false
  • String → null

Example:

class Demo {
    int num;
    String text;

    public static void main(String[] args) {
        Demo d = new Demo();
        System.out.println(d.num);   // 0
        System.out.println(d.text);  // null
    }
}
Enter fullscreen mode Exit fullscreen mode

Important Point:

If you create any constructor manually, the compiler will NOT create the default constructor.


2. No-Argument Constructor (User-defined Default-like Constructor)

This is a constructor written by the programmer with no parameters.

class Example {
    int x;

    Example() {
        x = 100;
    }

    public static void main(String[] args) {
        Example e = new Example();
        System.out.println(e.x);
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Parameterized Constructor

A constructor that accepts parameters to initialize values.

class Product {
    String name;
    int price;

    Product(String n, int p) {
        name = n;
        price = p;
    }

    public static void main(String[] args) {
        Product p1 = new Product("Pen", 10);
        System.out.println(p1.name + " " + p1.price);
    }
}
Enter fullscreen mode Exit fullscreen mode

Default Constructor vs User-Defined Constructor

Feature Default Constructor User-defined Constructor
Created by Compiler Programmer
Parameters No Can have parameters
Control Limited Full control
Initialization Default values Custom values

When is Default Constructor Used?

  • When no constructor is written in the class
  • When objects need simple initialization
  • During object creation in frameworks and libraries

Top comments (0)