• Creating Constructors

    We will now go deeper in java. Each java class contains a constructor. Constructors are just like methods that are being called when initializing a class.

    Creating Constructors

    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 atleast one constructor. If a class file doesn’t define a constructor, it will use the constructor of its superclass. 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 superclass 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();
    }
    }

    We have a class named 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. Only one constructor will be called for every instance. 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”.

    Related Post