Constructor in Java
By: Subhadeep Ghosh
Tags: Programming, Java, Coding, Codingbootcamp, Programming Languages

Introduction:

Java allows objects to initialize themselves when they are created.This automatic initialization is performed through the concept of constructor.

Properties of Constructor:

A constructor initializes an object immediately after creation

Constructor in java is considered as a method which has no return type

This method has same name with the class name in which it is declared

A constructor is immediately called after creation of object

package constructor;

public class InstaPage {

String pageName;

String pageType;

InstaPage(){//constructor

}

public static void main(String[] args) {

InstaPage page1=new InstaPage();

System.out.println(page1.pageName+” “+page1.pageType);

}

}

In the above code, you can see, InstaPage() is the constructor which is called at the time of creation of page1 object. Now the output of the above code will be null null which are their default values. The constructor InstaPage() is called as default constructor because it does not take any argument.

Now the question is, if we don’t declare default constructor, is object creation is possible? Yes obviously. You can see so many programs in which default construct is not present. Actually every program contains default constructor. Confused? Let me show you

package constructor;

public class InstaPage {

String pageName;

String pageType;

public static void main(String[] args) {

InstaPage page1=new InstaPage();

System.out.println(page1.pageName+” “+page1.pageType);

}

}

This code will execute fine and give the same output as previous. Actually the twist is java compiler automatically invokes default constructor if no constructor is present. This is the theory.

Have a look into another program

package constructor;

public class InstaPage {

String pageName;

String pageType;

InstaPage(String name, String type){

pageName=name;

pageType=type;

}

public static void main(String[] args) {

InstaPage page1=new InstaPage(“letscode_365”,”Education”);

System.out.println(page1.pageName+” “+page1.pageType);

}

}

Lets see the difference,

InstaPage(String name, String type){

pageName=name;

pageType=type;

}

The above code segment is the difference. Unlike the previous one, this constructor takes arguments and initializes the instance variables. Through this at the time of object creation values are initialized. The output of the program is letscode_365 Education.

Simply this type of constructor is called parameterized constructor or you can say constructor which takes arguments is called parameterized constructor.

Lets have an another example,

package constructor;

public class InstaPage {

String pageName;

String pageType;

int postno;

InstaPage(String name, String type){

pageName=name;

pageType=type;

}

InstaPage(String name, String type,int number){

pageName=name;

pageType=type;

postno=number;

}

public static void main(String[] args) {

InstaPage page1=new InstaPage(“letscode_365”,”Education”);

InstaPage page2=new InstaPage(“AnotherPage”,”Marketing”,42);

System.out.println(page1.pageName+” “+page1.pageType);

System.out.println(page2.pageName+” “+page2.pageType+” “+page2.postno);

}

Look carefully into the above program. There are two InstaPage() constructors which have different numbers of arguments. This process is known as constructor overloading. It is a very interesting feature. So, you can say, if there are more than one constructor which have different arguments on the basis of their operations, then it is called constructor overloading.

An important point to remember:

package constructor;

public class InstaPage {

String pageName;

String pageType;

InstaPage(String name, String type){

pageName=name;

pageType=type;

}

public static void main(String[] args) {

InstaPage page1=new InstaPage(“letscode_365”,”Education”);

InstaPage page1=new InstaPage();

System.out.println(page1.pageName+” “+page1.pageType);

}

}

In the above, look at the highlighted part. If you do this in the above program, it will generate a compilation error because when you declare a parameterized constructor then without declaring default constructor you cannot create any default object.

Read more:

this keword in java.

Encapsulation in java.