Home » Constructor in Java

Constructor in Java

Constructor in Java

Introduction

Constructors are unique methods used to initialize objects in Java. They are called when an object of a class is created and have the same name as the class. Constructors are vital for setting initial values for object attributes and performing any setup required before the object is used.

Types of Constructors

  • Default Constructor: A constructor that takes no arguments. If no constructor is defined in a category, the Java compiler automatically presents a default constructor.
  • Parameterized Constructor: A constructor that takes one or extra arguments. It permits for initializing objects with specific values when they are created.

Syntax of Constructors

Default Constructor:

class MyClass {
    // Default constructor
    MyClass() {
        // Initialization code
    }
}
Java

Parameterized Constructor:

class MyClass {
    int x;

    // Parameterized constructor
    MyClass(int y) {
        x = y;
    }
}
Java

Examples

Default Constructor

class Car {
    String model;
    int year;

    // Default constructor
    Car() {
        model = "Unknown";
        year = 0;
    }

    void display() {
        System.out.println("Model: " + model + ", Year: " + year);
    }

    public static void main(String[] args) {
        Car car = new Car(); // Calls default constructor
        car.display();
    }
}
Java

Output

Model: Unknown, Year: 0
SQL

Parameterized Constructor

class Car {
    String model;
    int year;

    // Parameterized constructor
    Car(String m, int y) {
        model = m;
        year = y;
    }

    void display() {
        System.out.println("Model: " + model + ", Year: " + year);
    }

    public static void main(String[] args) {
        Car car = new Car("Toyota", 2021); // Calls parameterized constructor
        car.display();
    }
}
Java

Output

Model: Toyota, Year: 2021
YAML

Constructor Overloading

Java allows constructor overloading, which means you may have a couple of constructors in a category with one-of-a-kind parameter lists. This affords flexibility in initializing objects in various methods.

class Car {
    String model;
    int year;

    // Default constructor
    Car() {
        model = "Unknown";
        year = 0;
    }

    // Parameterized constructor
    Car(String m, int y) {
        model = m;
        year = y;
    }

    // Another parameterized constructor
    Car(String m) {
        model = m;
        year = 2020; // Default year
    }

    void display() {
        System.out.println("Model: " + model + ", Year: " + year);
    }

    public static void main(String[] args) {
        Car car1 = new Car();
        Car car2 = new Car("Honda", 2022);
        Car car3 = new Car("BMW");

        car1.display();
        car2.display();
        car3.display();
    }
}
Java

Output

Model: Unknown, Year: 0
Model: Honda, Year: 2022
Model: BMW, Year: 2020
YAML

Key Points

  • Constructor Name: Must be the same as the magnificence call.
  • No Return Type: Constructors do not have a go back kind, no longer even void.
  • Invocation: Constructors are routinely known as while an item is created.
  • Overloading: You will have multiple constructors with extraordinary parameter lists within the same class.

Conclusion

Constructors are a essential a part of Java, used for initializing gadgets and setting preliminary values. Understanding constructors and a way to use them successfully allows you to create flexible and strong Java packages.

Frequently Asked Questions

1. What is a constructor in Java?

A constructor is a special technique used to initialize items. It has the same name as the magnificence and no return kind.

2. Can a class have multiple constructor?

Yes, a class can have a couple of constructors with one-of-a-kind parameter lists. This is known as constructor overloading.

3. What is the difference between a constructor and a method?

A constructor is used to initialize an item and does now not have a return kind, while a technique is used to carry out actions and has a go back type.