Call: +91-9992448483| Email: admin@pyarb.com

Constructors in Java


Constructor in Java

Here In this post we will discuss about Constructors in Java. In the preceding posts we have discussed about class and instance variables.

What is Constructor in Java?

Constructor is a special  method to initialize objects having same name as class name. I’ll describe some points about Constructors and try to illustrate these points with the help of code. Look at following points.

  1. Constructors must have same name as Class Name.
  2. Constructors never have return type.
  3. Constructor is called automatically at the time of object creation.
  4. If You do not define a constructor, Then compiler automatically generate a constructor with empty implementation.
Constructor in Java
Constructor in Java

Types Of Constructors in Java

  1. Default constructor
  2. Parameterized constructor

Default Constructor:

The constructor having no argument is Default Constructor in Java. Automatically generated constructor is also a type of default constructor.

Lets have a code below:

Example 1:

Now type javap command with class name as shown in following screenshot.

Constructor in Java - Demo javap command

Take a bit special care in this example. Compile the program before using javap tool. In above output we can see a Default constructor generated by compiler during compilation.  And object is created by virtue of default constructor.

Parameterized Constructor:

Constructor with parameters is called parameterized constructors. We need to pass these parameters with new operator at the time of object creation. Generally parameters passed to constructor are used to initialize instance variables in the class.

There may be more than one constructors in a java class. It means we can apply concept of overloading in Constructors in Java.

Lets have an example code again:

Example 2:

Output of this code will appear like this.

Output

Output Constructors in Java
Output Constructors in Java

Object creation
Object creation with params 12

 Some more Points about Constructors in Java

  1. Constructor do not have return type but it returns an instance of that class.
  2. If you use return type either void or any other, then that method will not treated as constructor. It will be treated as general method.
  3. You can apply any Java access modifier with constructor.
  4. If you are going to extend some class and want to use constructor in derived class, then you must define a default constructor on super class.
  5. Constructor is not regarded as member of class, Hence can not be called with object or class name.

Ok guys, I hope this article is helpful for you for having better understanding of Constructors in Java.

Summary
Introduction to Constructors in Java
Article Name
Introduction to Constructors in Java
Description
Constructor is a special  method to initialize objects having same name as class name.
Author
Publisher Name
PRB - PYarB.com
Publisher Logo

Have any Question or Comment?

Leave a Reply

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