• Creating 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 Constructors. Each java class contains a constructor and are just like methods that are being called when initializing a class.

    How to create 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, it will use the constructor of its super class. If it is not a subclass of any other class, it will be set to default which it will be a subclass of the Object class. You will learn more about super class and 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 in the past tutorial that 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 create 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, lets go back again to our main method where we have left. The next line myClass.addNumbers() will then be executed. This line 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 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. Lets see what will happen:

    1. First, in our main method, we have create 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 integer. The first constructor, public MainClass(), is not suitable because it doesn’t require any parameters. The second constructor match 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 initialized the variable firstNum and secondNum to 20 and 30 respectively.
    3. We then go back to our 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 to the default constructor. Next on our tutorial is how to implement inheritance by using the extends keyword.

    Related Post