Java Constructor

You have already learned the basics of Java including the fields and methods, the if statement, the loops and the switch statements. We will now go deeper in java by teaching how to create a Java Constructor. Each java class contains a constructor and is just like methods that are being called when initializing a class.

How to Create a Java Constructor

Constructors are defined with a modifier and the class name plus opening and closing parenthesis:

public class MainClass {
    public MainClass() {
        //this is a constructor
    }
}

All class has at least one constructor. If a class file doesn’t define a constructor, a default one will be created for you. A constructor should ultimately call the superclass constructor using the method super() before executing its own logic. You will learn more about the superclass and the subclass in the preceding chapters.

Constructors are the first method that will be called when initializing a class. For example,

public class MainClass {

    public int firstNum;
    public int secondNum;

    public MainClass() { //this is the constructor
        firstNum = 5;
        secondNum = 10;
    }

    public void addNumbers(){
        int answer = firstNum + secondNum;
        System.out.println(“The sum of two numbers is: “+answer);
    }

    public static void main(String args[]){
        //Create new instance of MainClass
        MainClass myClass = new MainClass();
        myClass.addNumbers();
    }
}

Above is our class MainClass. Inside our class, we have the constructor, the addNumbers method, and our main method. Remember that in the past tutorial, the first method that is being called is always the main method. Here’s what will happen if we run our program.

  1. First, it will call our main method. Inside our main method, we have created a new instance of MainClass and name it myClass. By executing this line, our constructor has also been called and will process whatever statements written inside our constructor.
  2. The constructor public MainClass() will then be called and will initialize our variable firstNum and secondNum to 5 and 10 respectively.
  3. After initialization, and calling our constructor method, it will go back again to our main method. The next line myClass.addNumbers() will then be executed. This will call our method addNumbers and will add our firstNum and secondNum and print the answer to the console.
  4. The output of our program will be “The  sum of two numbers is: 15”.

Multiple Java Constructors

In java, you can actually create multiple constructors. The compiler will just find the matching constructor upon instantiation. Consider the example below:

public class MainClass {

    public int firstNum;
    public int secondNum;

    public MainClass() { //this is a constructor
        firstNum = 5;
        secondNum = 10;
    }
    public MainClass(int x, int y) { //this is also a constructor
        firstNum = x;
        secondNum = y;
    }

    public void addNumbers() {
        int answer = firstNum + secondNum;
        System.out.println(“The sum of two numbers is: “+answer);
    }

    public static void main(String... args) {
        //Create new instance of MainClass
        MainClass myClass = new MainClass(20, 30);
        myClass.addNumbers();
    }
}

In the above example, we have two constructors. One without parameters and the other one that requires two integers. Let’s checked what happened:

  1. First, in our main method, we have created a new instance of MainClass. We can see that we had added a parameter in MainClass upon instantiation, MainClass(20, 30). The compiler will then search for the matching constructor that requires two integers. The first constructor, public MainClass(), is not suitable because it doesn’t require any parameters. The second constructor matches the required parameters which are two integers and therefore, that constructor will be called.
  2. The constructor public MainClass(int x, int y), will then be executed and will initialize the variable firstNum and secondNum to 20 and 30 respectively.
  3. After that, the execution will return to the main method. The line myClass.addNumbers, will be called and add our firstNum and secondNum and will produce an output “The sum of two numbers is 50”.

That’s it. Practice yourself to create a java class and create your own java constructor instead of relying on the default constructor. Next on our tutorial is how to implement inheritance by using the extends keyword.

Share this tutorial!