Java

What is a constructor in Java

Classes are the core concept of OOP(object-oriented programming). Java offers the concept of constructors that is directly associated with the classes as constructors hold exactly the same name as the class name. In java, the prime objective of constructors is to initialize the class objects. A java class can have several constructors, but their definition must be different in terms of parameters.

This article will present a comprehensive overview of the Java constructors and to do so, it will explain the below-listed concepts:

So, let’s get begin!

What is a Java Constructor

It is a special method that holds exactly the same name as the class name but doesn’t keep any return type. When someone creates the object of a class, the default constructor of that class is invoked/called automatically. The java constructors are used to initialize the objects, and in java, constructors may or may not take the parameters.

Types of Java Constructors

In java, constructors are of the following types:

  • default constructor.
  • parameterized constructor.

Default Constructor

During the program execution, the Java compiler creates a constructor(without arguments) for every single class automatically known as default constructor. The below snippet will show the basic syntax of the Java constructor:

classClassName {
ClassName () {
// code
  }
}

Here, the constructor’s name is exactly the same as the class name and it doesn’t have any return type.

Example1

The below code block will assist you in understanding the working of a constructor:

publicclassConstructDemo {
    String empName;
intempId;

ConstructDemo() {
System.out.println("Constructor Invoked!!");
empName = "Joe";
empId = 13;
    }

publicstaticvoidmain(String[] args) {
ConstructDemo object = newConstructDemo();
System.out.println("Employee name: " + object.empName);
System.out.println("Employee ID: " + object.empId);
    }

}

The above code block executed the following tasks:

  • Created two class attributes.
  • Initialized the attributes within the constructor.
  • Created an object using the new operator.
  • Accessed the values of class attributes using the class object and printed them on the console:

The above snippet verifies that when we create an object, the constructor is invoked automatically.

Parameterized Constructor

The constructors that can accept multiple values/parameters as arguments are referred to as the parameterized constructors. The snippet given below will show the basic syntax of the parameterized constructor:

classClassName {
ClassName (int val1, int val2, int val3) {
// code
  }
}

Here, ClassName(int val1, val2, val3) is a parameterized constructor that accepts three values/parameters.

Example2

The below-given code block will provide the detailed understanding of parameterized constructor:

publicclassParameterizedConstruct {
int x, y;
ParameterizedConstruct(int val1, int val2) {
System.out.println("Constructor Invoked!!");
  x = val1;
  y = val2;
 }
voidproduct(){
int pro;
  pro = x*y;
System.out.println("Result: " + pro);
 }
publicstaticvoidmain(String[] args) {
ParameterizedConstruct object = newParameterizedConstruct(12, 10);
object.product();
 }
}

The above code block executed the following tasks:

  • Created two class attributes.
  • A parameterized constructor.
  • Initialized the attributes within the constructor.
  • Created a user-defined method “product()”.
  • Performed multiplication within that method.
  • Created an object and passed the values for the parameterized constructor.
  • Invoked the product() method using object of the class:

The output proves the appropriateness of the parameterized constructor.

Conclusion

In Java, constructor is a special method that holds exactly the same name as the class name but doesn’t keep any return type. When someone creates the object of a class, the default constructor of that class is invoked/called automatically. The java constructors are used to initialize the objects, and in java, constructors may or may not take the parameters. The constructor which didn’t take any parameters is called the default constructor while the construct with parameters are known as parameterized constructors. This post explained the different aspects of java constructors, and for a profound understanding, it provided a couple of suitable examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.