Constructor in Java with Examples

In this tutorial, we will discuss constructors in Java in detail, including how to create and use them in our code. We will also provide examples to illustrate their usage.


In Java, a constructor is a special type of method that is used to create and initialize an object. It has the same name as the class in which it is defined, and it is called when an object is created using the new keyword.


For example, consider the following class definition:

java
public class Student {
  private String name;
  private int age;

  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

In this example, the Student class has a constructor with the signature public Student(String name, int age). This constructor has two parameters: a String named name and an int named age. The constructor initializes the name and age fields of the Student object with the values of the name and age parameters.


1. Creating an Object with a Constructor:

To create an object using a constructor, we need to use the new keyword followed by the class name and a set of parentheses containing any required parameters. For example:

java
Student s = new Student("John Smith", 20);

In this example, the Student constructor is called with the values "John Smith" and 20 as parameters. This creates a new Student object with the name "John Smith" and the age 20.

Note that the parameters we pass to the constructor must match the data types and order of the parameters defined in the constructor signature.


2. Default Constructors in Java:

If we do not define any constructors in a class, Java will automatically create a default constructor for us. This constructor does not have any parameters and it does not perform any initialization.


For example, consider the following class definition:

java
public class Student {
  private String name;
  private int age;
}

In this example, the Student class does not have any constructors defined. As a result, Java will automatically create a default constructor for us. This constructor can be called using the new keyword, as follows:

java
Student s = new Student();

This creates a new Student object with the default values for the name and age fields (i.e., null and 0, respectively).


3. Overloading Constructors in Java:

In Java, it is possible to define multiple constructors for a single class, as long as they have different parameter lists. This is known as constructor overloading.


For example, consider the following class definition:

java
public class Student {
  private String name;
  private int age;

  public Student(String name) {
    this.name = name;
    this.age = 0;
  }

  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

In this example, the Student class has two constructors. The first constructor has a single String parameter and sets the age field to 0, while the second constructor has both a String and an int parameter and sets both the name and age fields.


When calling a constructor, Java will automatically choose the correct version based on the types and number of parameters we pass in. For example:

java
Student s1 = new Student("John Smith"); // calls the first constructor
Student s2 = new Student("Jane Doe", 25); // calls the second constructor

In this example, the first call to the Student constructor uses a single String parameter, so the first constructor is called. The second call uses both a String and an int parameter, so the second constructor is called.


4. Conclusion

Constructors are an important part of object-oriented programming in Java, as they allow us to create and initialize objects. By understanding how to create and use constructors in our code, we can greatly improve the structure and maintainability of our programs.