Java Constructor

Java Constructors Java Constructor

In this tutorial, I will be explaining about Java Constructor. Why constructors are being used in java programming, what are the different types of constructor, how to implement constructors in our program and Some important things to remember about constructors in java.

Java Constructors



But before that we need to understand, what Java Constructor actually is ?

What is Java Constructor

Java constructor is responsible of creating an Object, initializing the default values of data members, providing object id to reference variable in Java.

Let’s take a real world example to understand constructor better.

Let’s take an example of “God”, God is the the creator of everything. God creates us, we are the living objects on earth. God creates us, initializes our default properties like height, weight, skin etc.

Some points to remember before creating constructor in Java

  1. The name of the constructor should be same as class name.
  2. You can not return anything from the constructor.
  3. Constructors can be public, protected, private and default.
  4. Constructors are special methods in java, they works look like normal methods but they can not be accessed via Objects.
  5. If you create a single constructor in your program then java default constructor will not be available for you. You need to create default constructor on your own.
  6. You can overload constructors.
  7. Super class constructor is always executed before child class constructor.
  8. You can call constructor inside another constructor.
  9. You can not specify static, final or synchronized to constructor.
  10. Constructor should be the first statement inside different constructor or method.

Internal Working

Look at the below example on how we create and invoke constructor in Java.

class Demo {

     public static void main(String arg[]) {
          Demo d = new Demo();
     }
}

If you look at the above example, you did not specify any constructor in your code. So Java will use, it’s no argument, default constructor. Below steps will be performed for Object creation.

  • “Demo d” :  New Reference variable will be created. But for now it does not hold any object reference. It is empty.
  • Compiler evaluates that “new” keyword is being used. Then it creates the memory space in Heap area for the object.
  • After space have been allocated to the Object, then Java compiler checks for the Constructor. If you don’t specify any constructor in your code. Then compiler will invoke it’s default constructor. And default values will be assigned to it’s data members.
  • After assigning the default values to data members of the class. Constructor returns the object reference id to the reference variable. So now reference variable will be able to locate the object in Heap area.


Types of Constructors in Java

There are different types of constructors in java as below. We will discuss each and every constructor with simple examples.

Note*: Default and parameterized constructors are the main types of constructor in java.

  1. default constructor
  2. parameterized constructor
You also need to understand below as well, remember they are not any type. They are just an implementation in java.
  1. constructor overloading
  2. copy constructor

let’s first understand what is default constructor and what’s happening internally.

What is Default Constructor

Default constructor is provided by Java when you don’t specify any constructor in your program. Default constructor accepts no parameter.

Basic Example

class Demo {
     
      public Demo() {
         System.out.println("I am default constructor.");
      }

      public static void main(String arg[]) {
         Demo d = new Demo(); // I am default constructor will be the output.
      }
}

If we specify default constructor in our program. Then Java doesn’t provide any default constructor automatically, it will use constructor defined in program.

Parameterized Constructor

In Parameterized Constructor, you can pass argument to the constructor. The reason for passing arguments to constructor is that, when Object is created, all the data members are assigned it’s default value like int will hold 0, float will hold 0.0, boolean will have false value and String will hold null.

So if you don’t want to use these values and want to assign your own values during object creation. You can use parameterized constructor for that purpose.

Simple Example


class Demo {
      private int a;
      private boolean b;

      public Demo() {
      }

      public Demo(int a, boolean b) {
         this.a = a;
         this.b = b;
      }

      public void printValues() {
         System.out.println("Value of a : " + a + " and value of b : " + b);
      }

      public static void main(String arg[]) {
         Demo d = new Demo(3, true);

         d.printValues(); // Value of a : 3 and value of b : true ; will be the output.
      }
}

Overloaded Parameterized Constructors in java

We can create multiple parameterized constructors in java. Remember, there can only be a single default constructor in java but can be multiple parameterized constructors.

Note*: We will discuss about overload later but for now you need to understand that overloading means creating multiple copies of same method but parameter data type and number of parameter must be different.

There are some rules for creating overloaded parameterized constructors.
  • Name of the constructor should be same as class name.
  • Parameter should always be different in numbers and data types.
  • JVM will invoke the constructor based on data type of the constructor and number of arguments you defined and passed during object creation.

Simple Example

class Demo {
      private int a;
      private boolean b;

      public Demo() {
      }

      public Demo(int a, boolean b) {
         this.a = a;
         this.b = b;
      }

      public Demo(boolean b, int a) {
         this.b = b;
         this.a = a;
      }

      public void printValues() {
         System.out.println("Value of a : " + a + " and value of b : " + b);
      }

      public static void main(String arg[]) {
         Demo d1 = new Demo(3, true);
         d1.printValues(); // Value of a : 3 and value of b : true ; will be the output.

         Demo d2 = new Demo(false, 22);
         d2.printValues(); // Value of a : 22 and value of b : false ; will be output.
      }
}

Copy Constructor

There is nothing like copy constructor in java like in C++.

Copy constructor is nothing but a concept of creating a copy of existing object in java programming. In this concept, you create a parameterized constructor which accepts existing object as an argument.

Simple Example

class Demo {
      private int a;
      private boolean b;

      public Demo() { }

      public Demo(int a, boolean b) {
         this.a = a;
         this.b = b;
      }

      public Demo(Demo copy) {
         this.a = copy.a;
         this.b = copy.b;
      }

      public void printValues() {
         System.out.println("a : " + a + " b : " + b);
      }

      public static void main(String arg[]) {
         Demo d = new Demo(23,true);
         d.printValues(); // a : 23 b : true

         Demo copy = new Demo(d);
         copy.printValues(); // Will output same as d, a : 23 b : true
      }
}

What is happening above

  • We have created two parameterized constructors one for assigning default values to data members and another for copying the values of existing object.

  • An object “d” is created with default values 23 and true by calling first parameterized constructor.

  • Then we passed object “d” to second parameterized constructor which will use the values of existing object to initialize new object.

One thought on “Java Constructor”

Leave a Reply

Your email address will not be published. Required fields are marked *