JAVA Tutorial



CONSTRUCTORS IN JAVA


Constructors in Java

In Java, a constructor is a special method used to initialize objects. It is called automatically when an object of a class is created. The constructor is used to set the initial state of an object by assigning values to its attributes. Let's dive into the details!

What is a Constructor?

A constructor is a block of code that gets executed when an instance of a class is created. It has the same name as the class and does not have a return type.

Constructor Syntax:

class ClassName {
    // Constructor
    public ClassName() {
        // Initialization code here
    }
}
  

The constructor is called when you create an object using the `new` keyword, like this: `ClassName obj = new ClassName();`

Types of Constructors in Java

In Java, there are two main types of constructors:

1. Default Constructor

A default constructor is a constructor that doesn't take any parameters. If no constructor is defined, Java provides a default constructor, which initializes the object with default values.

class Car {
    String model;
    int year;

    // Default Constructor
    public Car() {
        this.model = "Unknown";
        this.year = 0;
    }
}
  

2. Parameterized Constructor

A parameterized constructor allows you to pass arguments to initialize an object with specific values at the time of creation.

class Car {
    String model;
    int year;

    // Parameterized Constructor
    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }
}
  

How Constructors Work

When you create an object using the new keyword, the constructor is automatically invoked to initialize the object. Let's look at a practical example:

Example: Using Constructors

// Define a class named Car
class Car {
    String model;
    int year;

    // Constructor to initialize the model and year
    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }

    // Method to display the details
    public void displayDetails() {
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }
}

// Main class to test constructors
public class Main {
    public static void main(String[] args) {
        // Creating an object of Car class using a parameterized constructor
        Car myCar = new Car("Toyota", 2022);
        
        // Calling a method to display car details
        myCar.displayDetails();
    }
}
  

In this example, the `Car` class has a parameterized constructor, which initializes the object's attributes with the values passed when the object is created. The `displayDetails()` method then prints those values.

Constructor Overloading

Java allows constructor overloading, which means you can have multiple constructors with different parameter lists. This allows you to create objects in different ways.

Example: Constructor Overloading

class Car {
    String model;
    int year;

    // Default Constructor
    public Car() {
        this.model = "Unknown";
        this.year = 0;
    }

    // Parameterized Constructor
    public Car(String model, int year) {
        this.model = model;
        this.year = year;
    }

    // Method to display car details
    public void displayDetails() {
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }
}

// Main class to test constructor overloading
public class Main {
    public static void main(String[] args) {
        // Creating an object using the default constructor
        Car defaultCar = new Car();
        defaultCar.displayDetails();
        
        // Creating an object using the parameterized constructor
        Car myCar = new Car("Honda", 2023);
        myCar.displayDetails();
    }
}
  

Key Points to Remember

  • Constructor Name: The constructor's name must match the class name.
  • No Return Type: A constructor does not have a return type, not even void.
  • Initialization: Constructors are used to initialize object attributes with values.
  • Overloading: You can overload constructors to provide different ways of initializing objects.
  • Default Constructor: If no constructor is defined, Java provides a default constructor.

Quick Tip:

Practice by creating different classes with constructors for initializing various types of objects. Try overloading constructors and creating objects in different ways!


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review