Constructor in java

Constructor in java is block of code which allows you to create instance of the object. It does not have return type.
It has two main points

  • Constructor name should be same as class
  • Constructor should not have any return type else it will be same as method.

There are three types of Constructor in Java.

  • Default Constructor
  • No arg constructor
  • Parameterized constructor

How to call a constructor?

To call a constructor, you need to use the keyword new, followed by the name of class, followed by parameters if any.
For example: If you want to create the object of class Employee, you can call the constructor like this: new Employee()

Types of Contructor

Default Constructor:

When you do not provide the constructor for your class, JVM will create default constructor.It will not be visible to you, JVM will create it automatically while initializing object of the class.

Let’s check with the example of example:

As you can see here, we did not provide any constructor for this class but JVM will create default constructor in this case.
When you run above program, you will get below output:

John 20

no arg construtor

no arg constructor is constructor which you provide explicitly in the class and it does not have any argument.

When you call above program, you will get below output:

Calling no arg constructor
John
20

Parameterized constructor

When you pass arguments to the constructor, this type of constructor is called Parameterized constructor.

When you run above program, you will get below output:

Calling Parameterized constructor
John
20

If you provide Parameterized Constructor,then you need to be careful.
Let’s see below program:

If you notice, you will get compilation error at line no.38. Why so?
because if you create parameterized constructor in class, JVM won’t provide default constructor to you. If you do not write any constructor then only JVM will provide you default constructor.

Constructor chaining

Constructor chaining is the concept where child class calls the constructor of its parent class internally or explicitly.
Whenever you create an object in Java, its superclass constructor gets called. The compiler simply put super() in the constructor internally.
Let’s see with help of example:
Let’s say you have Person class with attribute name and you have child class named Employee which extends Person class.

When you run above program, you will get below output:

Calling Person constructor
Calling Employee class constructor
Martin

As you can see here, First Person constructor got called and it set name variable to John
Then Employee constructor got called which have overridden name variable to Martin.
That’s why we see a variable name as "Martin" in the end.

What if you want to explicitly call super class parameterized constructor

You can easily do it using super keyword.
Let’s see with the help of Example.

When you run above program, you will get below output:

Calling Person Parameterized constructor
Calling Employee class constructor
Employee’s name:John

What if you want to call another constructor of same class

If you want to call overloaded constructor of same class, you can use this keyword to do that.
For example:

When you run above program, you will get below output:

Calling No arg constructor
Calling Parameterized constructor
Employee’s name : John
Employee’s age : 20

Please note that this keyword used for calling overloaded constructor should be first statement in that constructor.

that’s all about Constructor in Java.


Related posts

Related Posts

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 25 September

    Reference Variable in Java

    Table of ContentsReference variableStatic Reference VariableInstance Reference VariableLocal Reference Variable In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

  • 26 March

    Static block in java

    Table of ContentsSyntaxExampleUse multiple static blocks In this post, we will see about how to implement static block in java. Here are some important points about static block in java. Static block is used for static initializations of a class Static block is executed only once when either you create an object of the class […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.