Constructor in Java

In this article. you will know what is Constructor in java? how is it works with some example?

What Is Constructor in java?

A constructor is a block of similar code to the Java method. When an instance of the class is created, a constructor is invoked. At that time of calling a constructor, memory for the object is assigned in the main memory.

Unlike Java methods, a constructor in java has the same name as that of the class and it doesn’t have any return type.

Whenever a new object is created by a new() keyword, a constructor is invoked. If the program doesn’t have any constructor then a default java constructor is called.

How to Create a Java Constructor?

There are some rules to create constructors in java. They are-

  • A Constructor name must be the same as the class name of its program.
  • A Java Constructor must have no explicit return type.
  • A constructor can not be static, final, abstract, and synchronized.

For Example:

class ConstExample{
  ConstExample() {
    // constructor body
  }
}

In this example, ConstExample() is a constructor. It has the same name as the class and doesn’t have any return type.

A Java Constructor is two types: 1. Default constructor (no-arg constructor) and 2. Parameterized constructor

1. Java Default constructor (no-arg constructor)

When no parameter is present in the constructor then that is called a default constructor. Default constructor provides the default values to the object like 0, null, etc. depending on the type.


public class Const {

	int a = 10;
	boolean b;

	  public static void main(String[] args) {

	    // A default constructor is invoked
	    Const obj = new Const();

	    System.out.println("Default Value: Nothing");
	    System.out.println("a = " + obj.a);
	    System.out.println("b = " + obj.b);
	  }

}

Output:

constructor in java

2. Parameterized constructor

A Constructor is known as Parameterized constructor when it accepts a specific number of parameters. It is used to produce different values for distinct objects,

class Student{  
    int id;  
    String name;  
    //creating a parameterized constructor  
    Student(int i,String n){  
    id = i;  
    name = n;  
    }  
    //method to display the values  
    void display(){System.out.println(id+" "+name);}  
   
    public static void main(String args[]){  
    //creating objects and passing values  
    Student s1 = new Student(1,"Harry");  
    Student s2 = new Student(2,"Ron");  
    //calling method to display the values of object  
    s1.display();  
    s2.display();  
   }  
}  
Output:
constructor in java

Constructor Overloading

Constructor Overloading in java works similarly to the java method of overloading. Constructor in java is just like a java method but without return type. Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.

public class Student {  
int id;  
String name;  
  
Student(){  
System.out.println("this a default constructor");  
}  
  
Student(int i, String n){  
id = i;  
name = n;  
}  
  
public static void main(String[] args) {  
//object creation  
Student s = new Student();  
System.out.println("\nDefault Constructor values: \n");  
System.out.println("Student's Id : "+s.id + "\nStudent Name : "+s.name);  
  
System.out.println("\nParameterized Constructor values: \n");  
Student student = new Student(10, "Jon");  
System.out.println("Student's Id : "+student.id + "\nStudent Name : "+student.name);  
}  
}  

Output:

this a default constructor


Default Constructor values: 


Student Id : 0
Student Name : null


Parameterized Constructor values: 


Student Id : 10
Student Name : John

FAQs:

Q. What is a non-static block in java?

Ans: Any code is written within the curly braces { } called a non-static block in java.

Q. When is a non-static block executed?

Ans: A non-static block is executed for each object that is created. It is executed before the constructor’s execution.

Leave a Reply

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