Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

A constructor in Java is a special method that is used to initialize an object. It is called when an object of a class is created. It is a block of code that is called when an instance of an object is created, and memory is allocated for the object.

It always has the same name as the class and does not have a return type (not even void).

Here is an example of a constructor in a class:

public class MyClass {
  int x;

  // Constructor for MyClass
  public MyClass() {
    x = 5;  // Initialize the variable x with the value 5
  }
}

In this example, MyClass is a constructor that is used to initialize the x variable to 5 when we create objects of the MyClass class.

You can also have constructors with parameters. For example:

public class MyClass {
  int x;

  // Constructor for MyClass
  public MyClass(int val) {
    x = val;  // Initialize the variable x with the passed value
  }
}

In this case, when you create a new object, you would pass in the value that you want x to be initialized to, like so:

MyClass myObj = new MyClass(10); // x in myObj is now 10

There are two types of constructors in Java:

  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.

In 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:

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:

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.

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.

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.

  • No labels