Java Constructor Tutorial – Why do we need Constructors ?

In this article we will discuss need and usage details of constructors in Java.

What is a Constructor ?

Constructor is a member function of the class that is used to initialize a newly constructed object i.e. initializing the member variables of object.

It has the same name as of the class.

Why do we need Constructors ?

Whenever we create an object in Object Oriented Methodology, we first need initialize its member variables. For example, we have a class Game with name and level as its member variables. Now suppose we created an object of this class Game, but forgot to initialize it. In that case its name and levels for this object will be garbage and will give undesired result when we call other member functions that uses them.

As initialization is must for an object before using it, therefore in Java language support for Constructors has been added.

Whenever we create a new object in Java, first of all its Constructor is called to initialize its value.

[showads ad=inside_post]

 

For example, we have a class Game, lets create an object of it,

Game obj = new Game();

Before this new object is assigned to reference obj, its constructor will be called internally.

Lets see the complete example,

package thispointer.java.examples.constructors;

public class Game {

	private String mGameName;
	private int mLevels;
	/*
	 * Default Constructor
	 */
	public Game() {
		this.mGameName = "Default Name";
		this.mLevels =1;
		System.out.println("Game::Default Constructor");
	}
	
	public String getGameName() {
		return mGameName;
	}
	public int getmLevels() {
		return mLevels;
	}
	public static void main(String[] args) {
		Game obj = new Game();		
		System.out.println(obj.getGameName() + "::" + obj.getmLevels());
	
	}

}

Output:

Game::Default Constructor
Default Name::1

Constructors are of 2 Types

1.) Default Constructor:

Constructor that take zero argument is called Default Constructor. In the above example we have defined a default Constructor i.e.

public Game() 
{
	this.mGameName = "Default Name";
	this.mLevels =1;
	System.out.println("Game::Default Constructor");
}

 

2.) Parameterized Constructors:

Constructor that take arguments are called parameterized Constructors. i.e.

public Game(String gameName, int levels) {
		this.mGameName =gameName;
		this.mLevels =levels;
		System.out.println("Game::parameterized Constructor");
	}

 

Lets add this in to the above class i.e.

package thispointer.java.examples.constructors;

public class Game {

	private String mGameName;
	private int mLevels;
	/*
	 * Default Constructor
	 */
	public Game() {
		this.mGameName = "Default Name";
		this.mLevels =1;
		System.out.println("Game::Default Constructor");
	}
	
	/*
	 * Parameterized Constructor
	 */
	public Game(String gameName, int levels) {
		this.mGameName =gameName;
		this.mLevels =levels;
		System.out.println("Game::parameterized Constructor");
	}
	
	
	public String getGameName() {
		return mGameName;
	}
	public int getmLevels() {
		return mLevels;
	}
	public static void main(String[] args) {
		Game obj = new Game();		
		System.out.println(obj.getGameName() + "::" + obj.getmLevels());
		
		Game obj_2 = new Game("ABC", 3);		
		System.out.println(obj_2.getGameName() + "::" + obj_2.getmLevels());

		
		
	}

}

Output:

Game::Default Constructor
Default Name::1
Game::parameterized Constructor
ABC::3

Return Type of Constructors

As Constructors are called internally at the time of object creation. So, nobody knows what value to expect in return from a Constructor and what to do with it. Therefore Constructors don’t return any value.

Constructor Overloading

Overloading a member function means defining different member functions with same but different argument list.

Constructors can be overloaded too i.e. defining different parameterized constructors will result in constructor overloading. Lets add a overloaded constructor in above class i.e.

/*
	 * Parameterized Overloaded Constructor
	 */
	public Game(String gameName) {
		this.mGameName =gameName;
		this.mLevels = 2;
		System.out.println("Game::Overloaded parameterized Constructor");
	}

Complete working code is as follows,

package thispointer.java.examples.constructors;

public class Game {

	private String mGameName;
	private int mLevels;
	/*
	 * Default Constructor
	 */
	public Game() {
		this.mGameName = "Default Name";
		this.mLevels =1;
		System.out.println("Game::Default Constructor");
	}
	
	/*
	 * Parameterized Constructor
	 */
	public Game(String gameName, int levels) {
		this.mGameName =gameName;
		this.mLevels =levels;
		System.out.println("Game::parameterized Constructor");
	}
	
	/*
	 * Parameterized Overloaded Constructor
	 */
	public Game(String gameName) {
		this.mGameName =gameName;
		this.mLevels = 2;
		System.out.println("Game::Overloaded parameterized Constructor");
	}
	
	public String getGameName() {
		return mGameName;
	}
	public int getmLevels() {
		return mLevels;
	}
	public static void main(String[] args) {
		Game obj = new Game();		
		System.out.println(obj.getGameName() + "::" + obj.getmLevels());
		
		Game obj_2 = new Game("ABC", 3);		
		System.out.println(obj_2.getGameName() + "::" + obj_2.getmLevels());

		
		
	}

}

 

1 thought on “Java Constructor Tutorial – Why do we need Constructors ?”

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top