Java Constructor


In Java, a constructor is a special method used to initialize objects when they are created. It is called automatically when an instance of a class is created and has the same name as the class. Constructors are used to set initial values for object attributes and perform any setup required for the object.

Key Characteristics of Constructors:

  1. Same Name as Class: A constructor must have the same name as the class it belongs to.

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

  3. Called Automatically: Constructors are called automatically when an object is instantiated using the new keyword.

  4. Can be Overloaded: You can have multiple constructors in a class with different parameter lists (method overloading).

Types of Constructors in Java:

  1. Default Constructor:

    • A constructor that does not take any parameters. If no constructor is explicitly defined, Java provides a default constructor that initializes object attributes to their default values.
  2. Parameterized Constructor:

    • A constructor that takes parameters, allowing you to pass values to initialize an object at the time of its creation.

Example of Constructors in Java

1. Default Constructor

class Car { String color; String model; // Default constructor Car() { color = "Red"; model = "Toyota"; } void displayInfo() { System.out.println("Car Model: " + model + ", Color: " + color); } } // Main class to demonstrate the default constructor public class Main { public static void main(String[] args) { Car myCar = new Car(); // Calls the default constructor myCar.displayInfo(); // Output: Car Model: Toyota, Color: Red } }

Explanation:

  • The Car class has a default constructor that initializes the color and model attributes.
  • When a new Car object is created, the default constructor is called, setting the initial values.

2. Parameterized Constructor

class Car { String color; String model; // Parameterized constructor Car(String c, String m) { color = c; model = m; } void displayInfo() { System.out.println("Car Model: " + model + ", Color: " + color); } } // Main class to demonstrate the parameterized constructor public class Main { public static void main(String[] args) { Car myCar = new Car("Blue", "Honda"); // Calls the parameterized constructor myCar.displayInfo(); // Output: Car Model: Honda, Color: Blue Car anotherCar = new Car("Green", "Ford"); // Another instance with different values anotherCar.displayInfo(); // Output: Car Model: Ford, Color: Green } }

Explanation:

  • The Car class has a parameterized constructor that accepts two parameters (c for color and m for model) to initialize the object's attributes.
  • When creating a Car object, you can pass specific values to the constructor to initialize the object with custom data.

Chaining Constructors

You can also call one constructor from another using this() to create constructor chaining. This is useful for reducing redundancy.

class Car { String color; String model; // Default constructor Car() { this("Red", "Toyota"); // Calls the parameterized constructor } // Parameterized constructor Car(String c, String m) { color = c; model = m; } void displayInfo() { System.out.println("Car Model: " + model + ", Color: " + color); } } // Main class to demonstrate constructor chaining public class Main { public static void main(String[] args) { Car myCar = new Car(); // Calls the default constructor myCar.displayInfo(); // Output: Car Model: Toyota, Color: Red } }

Explanation:

  • In this example, the default constructor calls the parameterized constructor using this("Red", "Toyota"), which initializes the object with default values.

Summary:

  • A constructor in Java is a special method used to initialize objects of a class.
  • Constructors can be default (no parameters) or parameterized (with parameters).
  • They have the same name as the class, do not have a return type, and can be overloaded to provide multiple ways to create an object.
  • Constructors help in setting initial values and performing necessary setup when an object is created.