Constructors and their types in Java

Constructor is a block of code that initializes the newly created object. In Java, the constructor looks like an instance method but it is not a method because it does not have a return type.

In short, both constructor and method are different, although people often refer to constructor as a special type of method in java.

The name of the constructor is given in the name of the class itself and in java code it is written like this –

public class MyClass{
//This is the constructor
MyClass(){

,
,
,

The thing to note here is that the name of the constructor here matches the name of the class and there is no return type in it.

How does a constructor work?

Let us take an example to understand the working of the constructor, suppose we have a class named myclass, we create the object of myclass like this –

also read

MyClass obj = new MyClass()

Here the new keyword creates an object of class myclass and invokes the constructor to initialize this newly created object.

A simple constructor program in java:

public class Hello
,
String name;
//constructor
Hello()
,
this.name = “ComputerHindiNotes”;
,
public static void main(String[] args)
,
Hello obj = new Hello();
System.out.println(obj.name);
,

Result (Output):

ComputerHindiNotes

Here we have created object of class Hello and named this object as obj. As you can see that the output is ComputerHindiNotes which we passed during initialization of constructor. This shows that the constructor was called when we created the object. In this example we have used this keyword which refers to the current object obj in this example. We will know about this keyword in the coming tutorial.

Types of Constructor

There are 3 types of Constructor

  1. default constructor
  2. no-argument constructor
  3. Parameterized constructor.

default constructor

If you do not implement any constructor in the class then there java compiler includes the default constructor in your code. You will not find it in your source code ( java file ) because it will be inserted at the time of compilation. The default constructor is found in the .class file.

This process is shown in the figure below:-

default contructor

If you use a constructor in your program, then the default constructor is not inserted into the program by the java compiler.

No-Argument constructor

A constructor without any arguments is known as no-argument constructor. The signature is usually the same as the default constructor, although the body can contain any code unlike the default constructor.

Although you can see some people claiming that default constructor and no-argument constructor are same, but in reality they are not the same, if you write public demo() {} in your class demo then it is called default The constructor cannot be called because you have written its code.

class demo
,
public Demo()
,
System.out.println(“This is a no argument constructor”);
,
public static void main(String args[],
new Demo();
,
,

This is a no argument constructor.

Parameterized constructor

Parameterized constructor is a type of constructor, which has some arguments (they can also be called parameters).

Example :-
public class Employee {

int empId;
String empName;

//parameterized constructor with two parameters
Employee(int id, String name){
this.empId = id;
this.empName = name;
,
void info(){
System.out.println(“Id: “+empId+” Name: “+empName);
,

public static void main(String args[],
Employee obj1 = new Employee(10245,”Chaitanya”);
Employee obj2 = new Employee(92232,”Negan”);
obj1.info();
obj2.info();
,
,
Output:
Id: 10245 Name: Chaitanya
Id: 92232 Name: Negan

In our example we have parameterized constructor with two parameter id, here two arguments are passed while creating object obj1 and obj2 so that after creation of object obj1 and obj2 this constructor is invoked.
Example 2 : parameterized constructor
class Example2
,
private int var;
//default constructor
public Example2()
,
this.var = 10;
,
// parameterized constructor
public Example2(int num)
,
this.var = num;
,
public int getValue()
,
return var;
,
public static void main(String args[],
,
Example2 obj = new Example2();
Example2 obj2 = new Example2(100);
System.out.println(“var is: “+obj.getValue());
System.out.println(“var is: “+obj2.getValue());
,
,
Output:
var is: 10
var is: 100

In this example we have two constructors, a default constructor and a parameter constructor. When we do not pass any parameter while creating object using new keyword then default constructor is called however when any parameter is passed then it is parameterized constructor call which matches the given list of parameters. is |
What if only parameterized constructor is implemented in the class?
class Example3
,
private int var;
public Example3(int num)
,
var=num;
,
public int getValue()
,
return var;
,
public static void main(String args[],
,
Example3 myobj = new Example3();
System.out.println(“value of var is: “+myobj.getValue());
,
,

Result (output): – In the above example there will be a compilation error, this is because the statement Example3 myobj= new Example3() in class Example3 is invoking the default constructor, which we have not written in the program. When you do not implement any constructor in your class then compiler includes default constructor in your code however when you implement any constructor then you do not get default constructor by compiler.
If you remove the parameterized constructor from the above code, the program will still run because then the default constructor will be included in your code by the compiler.

,