. . .


A constructor initializes an object with its creation. It has the same name as the name of its class. Once a constructor is defined, it is automatically called immediately the memory is allocated before the new operation completes. A constructor does not have any return type, its implicit return type is a class object. You can see constructor as a class type. Its job is to initialize the instance variables of an object so that the created object is usable just after creation.

Overloading Constructors

You may ask whether there can be more than one constructor in a class. Yes, there may be more than one constructor in a class but all the constructors in a class either will have different types of arguments or a different number of arguments passed to it. This is essential because without this it wouldn’t be possible to identify which of the constructors is invoked. Having more than one constructor in a single class is known as constructor overloading. In the program given below more than one constructor in a class, Student is defined.

Constructor Point

Constructor Point is a non-parameterized constructor. Both the objects p1 and p2 are created by using Point constructor and are initialized by the same value which is assigned during the definition of the constructor. This type of constructors is generally used for the initialization of those objects for which the initial value of instance variables is known. If values of the instance variables are given at the time of object creation, parameterized constructors are used for this.

Code example of Constructor:

public class Student {
	String name;
	int roll_no;
	String course;
	Student(String n, int r, String c) {
		name = n;
		roll_no = r;
		course =  c;
	void StudentInfo() {
		System.out.println("******* Student Information ********");
		System.out.println("Roll Number:"+roll_no);

	class StudentTest {
	public static void main(String[]args) {
		Student s1= new Student("Prosenjit Sarkar" ,191027469,"BCA" );
		Student s2= new Student("Mayaz Rahaman", 191027468 ,"MCA");
Please share the article

Leave a Reply

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

3 − 1 =