Java Constructor

Category: Java   Tags: Java, Java Basic, Java Constructor

A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. A constructor is automatically called when the object is created. A constructor have no return type. This is because the implicit return type of a class constructor is the class type itself. A constructor's job is to initialize the internal state of an object.

A Java Constructor Example

AddNumber.java

                          package com.tutorial.javabasic;

                          class AddNumber {
                              private int a;
                              private int b;

                              AddNumber() {
                                  this.a = 100;
                                  this.b = 200;
                              }

                              AddNumber(int a, int b) {
                                  this.a = a;
                                  this.b = b;
                              }

                              public int getSum() {
                                  return a+b;
                              }
                          }
                        

Here the class name is AddNumber and you can see we have two constructor. If you see it closely you will find that it has:

  • Same name as its class name
  • No return type
  • It can accept parameters.

In AddNumber class there are two constructor:

                          AddNumber() {
                              this.a = 100;
                              this.b = 200;
                          }
                        

This is a default constructor. A default constructor is called by default.

Inside the default constructor we are assigning 100 to a and 200 to b.

                          AddNumber(int a, int b) {
                              this.a = a;
                              this.b = b;
                          }
                        

This constructor has two parameter: int a and int b and this should be called explicitly.

Now we will show you how to use and call a Java constructor.

Main.java

                          package com.tutorial.javabasic;

                          public class Main {
                              public static void main(String s[]) {
                                  AddNumber calculation1 = new AddNumber();
                                  int sum1 = calculation1.getSum();
                                  System.out.println("Sum = " + sum1);

                                  AddNumber calculation2 = new AddNumber(10, 20);
                                  int sum2 = calculation2.getSum();
                                  System.out.println("Sum = " + sum2);
                              }
                          }
                        

Here AddNumber calculation1 = new AddNumber(); will call the default constructor AddNumber() { and set the value of a to 100 and b to 200. Calling calculation1.getSum(); will do calculation on 100 and 200 and so it will return 300.

AddNumber calculation2 = new AddNumber(10, 20); will call the parameter constructor AddNumber(int a, int b) { and set the value of a to 10 and b to 20. Calling calculation2.getSum(); will do calculation on 10 and 20 and so it will return 30.

Execution

If you run this program, output should be:

                            Sum = 300
                            Sum = 30