Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Default constructor: If you do not define a constructor in the class, compiler will create a default constructor in the class that initializes all instance variables with default values (null for objects, false for boolean, 0 for numeric values).

  2. Parameterized constructor: It's a constructor that accepts a list of parameters and initializes the instance variables using these values.

One last thing to note is the concept of constructor overloading, where a class can have multiple constructors with different parameter lists. The compiler differentiates these constructors by taking into account the number of parameters in the list and their typesIn Java, a constructor is a special method that gets called when an object is instantiated (created). Unlike regular methods, a constructor has no return type and its name must exactly match the name of the class it resides in. Constructors are primarily used for initializing the new object.

For example, if you have a Car class and you want to initialize a new car object with a specific color and brand, you can use a constructor to do so.

Here's a simple example:

Code Block
languagejava
public class Car {
    String color;
    String brand;

    // Constructor
    public Car(String color, String brand) {
        this.color = color;
        this.brand = brand;
    }
}

To create a new Car object, you would do the following:

Code Block
languagejava
Car myCar = new Car("Red", "Toyota");

In this example, when you create a new Car object using the new keyword, the constructor Car(String color, String brand) is called with the arguments "Red" and "Toyota". Inside the constructor, these arguments are used to initialize the color and brand fields of the new Car object.

Default Constructor

If you don't define any constructors for a class, Java automatically provides a default constructor with no parameters. This default constructor will initialize all instance variables with default values (e.g., 0 for numeric types, false for booleans, and null for objects).

Overloading Constructors

You can have multiple constructors in the same class, as long as their parameter lists are different. This is known as constructor overloading.

Code Block
languagejava
public class Car {
    String color;
    String brand;

    // First constructor
    public Car() {
        this.color = "Unknown";
        this.brand = "Unknown";
    }

    // Second constructor
    public Car(String color, String brand) {
        this.color = color;
        this.brand = brand;
    }
}

In this example, you can create a Car object either with no arguments, which will invoke the first constructor, or with color and brand arguments, which will invoke the second constructor.

Code Block
languagejava
Car defaultCar = new Car();  // Calls the first constructor
Car customCar = new Car("Blue", "Ford");  // Calls the second constructor

A constructor in Java initializes a newly-created object and gets called when the object is instantiated using the new keyword. You can have multiple constructors through overloading, and if you don't provide any constructors, a default one is automatically supplied.