Constructors are the special methods in Java which gets executed as soon as we create an object of the Class. To create a Constructor in Class, you need to create a method with name same as your Class Name. For example if you have created a Class with name Car then in order to provide constructor to this Class you need to create a method name Car

Constructors are usually used to do initialisations i.e. to provide default value to your Class member variables. Lets Consider an example below.

public class BankAccount {

    private String name;
    private double accountBalance;

    public BankAccount() {
        this.name = "Default Name";
        this.accountBalance = 0.0;
    }

    public double depositBalance(int balance) {
        this.accountBalance += balance;
        return this.accountBalance;
    }

    public double withDrawBalance(int balance) {
        this.accountBalance -= balance;
        return this.accountBalance;
    }

}

We have provided a method BankAccount() in Class with name BankAccount. Thus as soon as we create a object of this Class, the name on the account will be initialised to "Default Name" and balance will be set to 0.

Constructor Overloading

As we have seen the concept of method overloading, similar concept can be applied to Constructors as well. We can create multiple Constructors sharing the same name but different parameters. Consider the example below to understand Constructor overloading.

public class BankAccount {

    private String name;
    private double accountBalance;

    public BankAccount() {
        this.name = "Default Name";
        this.accountBalance = 0.0;
    }

    public BankAccount(String name) {
        this.name = name;
        this.accountBalance = 0.0;
    }

    public BankAccount(String name, double balance) {
        this.name = name;
        this.accountBalance = balance;
    }

    public double depositBalance(int balance) {
        this.accountBalance += balance;
        return this.accountBalance;
    }

    public double withDrawBalance(int balance) {
        this.accountBalance -= balance;
        return this.accountBalance;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getAccountBalance() {
        return accountBalance;
    }

    public void setAccountBalance(double accountBalance) {
        this.accountBalance = accountBalance;
    }

}

We have created three different Constructors for the BankAccount Class, let's create a Test Java Program to test our constructors.

public class BankAccountTest {

    public static void main(String args[]) {
        BankAccount account1 = new BankAccount();
        System.out.println("Account 1 Holder Name is " + account1.getName()
                + " and Account Balance is " + account1.getAccountBalance());
        BankAccount account2 = new BankAccount("Lisa Tondon");
        System.out.println("Account 2 Holder Name is " + account2.getName()
                + " and Account Balance is " + account2.getAccountBalance());
        BankAccount account3 = new BankAccount("Lisa Tondon", 50000.00);
        System.out.println("Account 2 Holder Name is " + account3.getName()
                + " and Account Balance is " + account3.getAccountBalance());
    }

}

If you run this code it will produce the following output.

Account 1 Holder Name is Default Name and Account Balance is 0.0
Account 2 Holder Name is Lisa Tondon and Account Balance is 0.0
Account 2 Holder Name is Lisa Tondon and Account Balance is 50000.0

Points to consider regarding Java Constructors

  • Name of the constructor should be the same as the class name.
  • A constructor doesn’t have a return type.
  • Unlike methods, constructors are not considered members of a class.
  • A constructor is called automatically when a new instance of an Class / new object is created.
  • Constructors can be overloaded just like normal java methods.
  • Constructors can be private. This facilitates Singleton Design Pattern.
Comments