Constructors in Java

What is a Constructor?

A **constructor** is a special method in Java that is used to initialize objects. It is automatically called when an object is created.

Characteristics of a Constructor

  • The constructor name must be the same as the class name.
  • It does not have a return type.
  • It is automatically called when an object is created.

Types of Constructors

Java has three types of constructors:

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor (not built-in, but can be implemented)

1. Default Constructor

A default constructor has no parameters and initializes default values.

public class DefaultConstructor {
    int x;

    // Default constructor
    public DefaultConstructor() {
        x = 10;
        System.out.println("Default constructor called!");
    }

    public static void main(String[] args) {
        DefaultConstructor obj = new DefaultConstructor();
        System.out.println("Value of x: " + obj.x);
    }
}

2. Parameterized Constructor

A parameterized constructor allows passing values during object creation.

public class ParameterizedConstructor {
    int x;

    // Parameterized constructor
    public ParameterizedConstructor(int value) {
        x = value;
    }

    public static void main(String[] args) {
        ParameterizedConstructor obj = new ParameterizedConstructor(20);
        System.out.println("Value of x: " + obj.x);
    }
}

3. Copy Constructor

Java does not have a built-in copy constructor, but we can create one manually.

public class CopyConstructor {
    int x;

    // Constructor
    public CopyConstructor(int value) {
        x = value;
    }

    // Copy constructor
    public CopyConstructor(CopyConstructor obj) {
        x = obj.x;
    }

    public static void main(String[] args) {
        CopyConstructor obj1 = new CopyConstructor(30);
        CopyConstructor obj2 = new CopyConstructor(obj1);
        
        System.out.println("Value of x in obj1: " + obj1.x);
        System.out.println("Value of x in obj2: " + obj2.x);
    }
}

Constructor Overloading

Just like methods, constructors can also be overloaded in Java.

public class ConstructorOverloading {
    int x;

    // Default constructor
    public ConstructorOverloading() {
        x = 5;
    }

    // Parameterized constructor
    public ConstructorOverloading(int value) {
        x = value;
    }

    public static void main(String[] args) {
        ConstructorOverloading obj1 = new ConstructorOverloading();
        ConstructorOverloading obj2 = new ConstructorOverloading(50);

        System.out.println("Value of x in obj1: " + obj1.x);
        System.out.println("Value of x in obj2: " + obj2.x);
    }
}

Difference Between Methods and Constructors

Feature Constructor Method
Purpose Used to initialize objects Used to perform operations
Return Type Does not have a return type Has a return type (void or data type)
Invocation Automatically called when an object is created Explicitly called

Conclusion

Constructors play a vital role in object initialization in Java. Understanding constructors helps in writing **better, cleaner, and optimized** Java programs.

📌 Next Topic: Inheritance in Java