Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

November 11, 2014 Categories: Core Java Basics. 2 Comments on Java Constructors

In this article we will look at constructors in Java. We will learn what is a constructor, use of a constructor in Java programs, characteristics of a constructor and different types of a constructor.





A constructor is a special method which has the same name as class name and that is used to initialize the objects (fields of an object) of a class. A constructor has the following characteristics:

  1. Constructor has the same name as the class in which it is defined.
  2. Constructor doesn’t have a return type, not even void. Implicit return type for a constructor is the class name.
  3. Constructor is generally used to initialize the objects of a class.


Syntax for creating a constructor is as shown below:



Use of Constructors


A constructor is used for initializing (assigning values to fields) the objects of a class. Can’t we use normal methods for initializing the fields of an object? Why do we require constructors? To make it clear what is the need for constructors, let’s consider an example of where we want to develop a Tic-Tac-Toe game as shown below:


Tic-Tac-Toe constructor


To make it simple, let’s not consider how to display graphics. We will consider only the general details. If you look at the above picture, we have to display a canvas or board (for example) which contains 3×3 = 9 squares. To display the canvas with 9 squares, let’s consider the following class definition:



The Square class will be something as shown below:



The driver program (which executes our logic classes) will be as shown below:



If you look at the above program, the task of display() method is to create 9 square blocks by internally calling createBlock() method of Square class. This whole process can be seen as initializing the board. Every time, when someone wants to play the game, the board has to be initialized (square blocks have to be created and displayed to the user).


To make the initialization process simple, constructors have been introduced whose sole purpose is to initialize the object. Now, let’s modify our previous classes to include constructors. The code is shown below:



The Square class will be as shown below:



The driver program (which executes our logic classes) will be as shown below:



If you look at the above code, the methods display() and createBlock() are gone. Remember that initialization might not be only simply assigning values to the fields of an object.


The purpose of a constructor is to initialize the objects such that they will be ready to be used in our program like the board object in above program.


Constructor Invocation


A constructor is invoked (called) automatically whenever an object is created using the new keyword. For example, in the above example, new TicTacToe() calls the constructor of TicTacToe class. If no constructor has been defined, Java automatically invokes the default constructor which initializes all the fields to their default values.


Types of Constructor


Based on the number of parameters and type of parameters, constructors are of three types:

  1. Parameter less constructor or zero parameter constructor
  2. Parameterized constructor
  3. Copy constructor


Parameter less constructor: As the name implies a zero parameter constructor or parameter less constructor doesn’t have any parameters in its signature. These are the most frequently found constructors in a Java program. Let’s consider an example for zero parameter constructor:



In the above example, Sqaure() is a zero parameter or parameter less constructor which initializes the side of a square to 4.


Parameterized constructor: This type of constructor contains one or more parameters in its signature. The parameters receive their values when the constructor is called. Let’s consider an example for parameterized constructor:



In the above example, the Square() constructor accepts a single parameter s, which is used to initialize the side of a square.


Copy constructor: A copy constructor contains atleast one parameter of reference type. This type of constructor is generally used to create copies of the existing objects. Let’s consider an example for copy constructor:



Now let’s an example which covers all three types of constructors:



Run the above program and observe the output.

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.


You can follow any responses to this entry through the RSS 2.0 feed.

This is the right blog for anybody who really wants to understand this topic Wonderful points you have mentioned here,. Its really a great and useful piece of information. Im glad that you shared this helpful information with us.
Please stay us up to date like this. Thanks for sharing.

Way cool! Some extremely valid points! I appreciate you writing this
write-up and also the rest of the website is really good.

Leave a Reply

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

Scroll Up