Java Programming Handbook

    Java Constructor

    In Java, a constructor is a special type of method that is used to initialize objects. It is automatically called when an object of a class is created. Constructors help in setting initial values for object attributes.

    Characteristics of a Constructor#

    • A constructor has the same name as the class.
    • It does not have a return type, not even void.
    • It is automatically invoked when an object is created.
    • Constructors can have parameters to initialize objects with different values.

    Types of Constructors in Java#

    1. Default Constructor#

    A default constructor is a constructor without parameters. If no constructor is defined in a class, Java automatically provides a default constructor.

    Example:#

    // Defining a class class Car { // Constructor Car() { System.out.println("Car object is created!"); } public static void main(String[] args) { // Creating an object of Car class Car myCar = new Car(); } }

    Output:#

    Car object is created!

    2. Parameterized Constructor#

    A parameterized constructor allows us to pass values while creating an object.

    Example:#

    // Defining a class class Car { String brand; int speed; // Parameterized Constructor Car(String b, int s) { brand = b; speed = s; } void display() { System.out.println("Brand: " + brand); System.out.println("Speed: " + speed + " km/h"); } public static void main(String[] args) { // Creating objects with different values Car car1 = new Car("Tesla", 250); Car car2 = new Car("BMW", 220); // Displaying object details car1.display(); car2.display(); } }

    Output:#

    Brand: Tesla Speed: 250 km/h Brand: BMW Speed: 220 km/h

    3. Copy Constructor#

    A copy constructor creates a new object by copying the values of an existing object.

    Example:#

    class Car { String brand; int speed; // Parameterized Constructor Car(String b, int s) { brand = b; speed = s; } // Copy Constructor Car(Car obj) { brand = obj.brand; speed = obj.speed; } void display() { System.out.println("Brand: " + brand); System.out.println("Speed: " + speed + " km/h"); } public static void main(String[] args) { Car car1 = new Car("Tesla", 250); Car car2 = new Car(car1); // Copying car1's values into car2 car1.display(); car2.display(); } }

    Output:#

    Brand: Tesla Speed: 250 km/h Brand: Tesla Speed: 250 km/h

    Constructor Overloading#

    Constructor overloading allows a class to have multiple constructors with different parameters.

    Example:#

    class Car { String brand; int speed; // Default Constructor Car() { brand = "Unknown"; speed = 0; } // Parameterized Constructor Car(String b, int s) { brand = b; speed = s; } void display() { System.out.println("Brand: " + brand); System.out.println("Speed: " + speed + " km/h"); } public static void main(String[] args) { Car car1 = new Car(); // Calls default constructor Car car2 = new Car("Audi", 240); // Calls parameterized constructor car1.display(); car2.display(); } }

    Output:#

    Brand: Unknown Speed: 0 km/h Brand: Audi Speed: 240 km/h

    Conclusion#

    Constructors are essential in Java for initializing objects efficiently. We learned about:

    • Default constructors (automatically created if no constructor is defined).
    • Parameterized constructors (used to assign values while creating objects).
    • Copy constructors (used to create a new object by copying values from another object).
    • Constructor overloading (multiple constructors with different parameters).

    Last updated on Apr 09, 2025