Java Constructor

A constructor in Java is a special method used to initialize objects. It is called automatically when an object of a class is created. The primary purpose of a constructor is to assign values to the fields (attributes) of the new object and set up any necessary initialization logic.

Characteristics of Constructors

1. Constructor Name: The name of the constructor must be the same as the class name.

2. No Return Type: Constructors do not have a return type, not even void.

3. Automatic Invocation: A constructor is automatically called when an object is created using the new keyword.

4. Overloading: You can have multiple constructors with different parameter lists (constructor overloading).

Types of Constructors

Default Constructor (No-argument constructor):

1. A constructor that doesn’t take any arguments and assigns default values to the fields of an object.

2. If no constructor is explicitly defined in a class, Java provides a default constructor automatically.

Parameterized Constructor:

A constructor that takes one or more arguments to initialize the fields of an object with specific values at the time of object creation.

Example of Default Constructor


class Employee {
    String name;
    int age;

    // Default constructor
    Employee() {
        name = "Unknown";  // Default value for name
        age = 18;        // Default value for age
    }

    // Method to display car details
    void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of the Employee class using the default constructor
        Employee emp1 = new Employee();
        emp1.displayInfo();  // Output: Name: Unknown, Age: 18
    }
}

Explanation:

1. The default constructor Employee() initializes the name and age fields with default values when a new object is created.

2. When we create emp1 using new Employee(), it automatically calls the default constructor.

Example of Parameterized Constructor:


class Employee {
    String name;
    int age;

    // Parameterized constructor
    Employee(String name, int age) {
        this.name = name;  // 'this' refers to the current object
        this.age = age;
    }

    // Method to display car details
    void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating objects with different initial values using the parameterized constructor
        Employee emp1 = new Employee("John", 35);
        Employee emp2 = new Employee("Tom", 30);
        
        emp1.displayInfo();  // Output: Name: John, Age: 35
        emp2.displayInfo();  // Output: Name: Tom, Age: 30
    }
}

Explanation:

1. The constructor Employee(String name, int age) takes two parameters and initializes the fields model and year with the provided values.

2. When we create emp1 and emp2, the constructor is called with the corresponding arguments (“John”, 35) and (“Tom”, 30), respectively.

Constructor Overloading

Constructor overloading means having multiple constructors with different parameter lists in the same class. This allows you to create objects in different ways.

Example: Constructor Overloading


class Employee {
    String name;
    int age;

    // No-argument constructor
    Employee() {
        name = "Unknown";
        age = 18;
    }

    // Parameterized constructor
    Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method to display car details
    void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating objects using different constructors
        Employee emp1 = new Employee();  // Calls the no-argument constructor
        Employee emp2 = new Employee("John", 35);  // Calls the parameterized constructor
        
        emp1.displayInfo();  // Output: Name: Unknown, Age: 18
        emp2.displayInfo();  // Output: Name: John, Age: 35
    }
}

Explanation:

1. We have two constructors in the Employee class: one that takes no arguments (default constructor) and one that takes two arguments (String name, int age).

2. This allows us to create a Employee object with default values or with custom values.

Constructor Chaining

Constructor chaining is the process of calling one constructor from another constructor within the same class or between classes. This is done using the this() keyword for calling another constructor in the same class or super() for calling a superclass constructor.

Example: Constructor Chaining


class Employee {
    String name;
    int age;

    // Constructor with parameters
    Employee(String name) {
        this(name, 35);  // Calls the second constructor with name and default age
    }

    // Constructor with both parameters
    Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method to display car details
    void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object using constructor chaining
        Employee emp1 = new Employee("John");  // Calls the constructor with one argument
        emp1.displayInfo();  // Output: Name: John, Age: 35
    }
}

Explanation:

1. The constructor Employee(String name) calls the constructor Employee(String name, int age) using this(name, 35).

2. This ensures that both constructors are chained, and year is initialized to 35 by default when only the model is provided.