You are currently viewing Constructor in java with Example

Constructor in java with Example

In this tutorial, we will learn about Java constructors/Constructor in java, their types, and how to use them.

What is a Constructor?

Constructor in Java is a block of code that is similar to a method. When a new instance of the class is created, it is called. Memory for the object is allocated in the memory when the constructor  is called. 

  • It is a type of method that is used to initialise an object.
  • At least one constructor is called every time an object is created with the new() keyword.
  • If there is no constructor available in the class, it calls the default constructor. In this case, the Java compiler automatically provides a default constructor.

In Java, there are two kinds of constructors:

  • Default constructor
  • Parameterized constructor.

Default Constructor

We haven’t created any constructors in this case. As a result, the Java compiler generates the default constructors automatically. The default constructors sets the default values for any uninitialized instance variables.

TypeDefault Value

Example: In this example, we’re going to write a no-arg constructor for the Car class. It will be called when the object is created.

class Car{  
// default constructor  
Car() {
System.out.println("Car is running");
//main method  
public static void main(String args[]){  
//calling default constructor  
Car b=new Car();  


Car is running

Java Parameterized Constructor

Parameterized Constructor – A constructor is referred to as a Parameterized Constructor if it accepts a specific number of parameters. When using a parameterized constructor for a class, initial values must be provided as arguments or the compiler will report an error.

Example: In this example, we created a constructor for the Sample class with two parameters.

class Sample{  
    int id;  
    String name;  
    //parameterized constructor  
    Sample(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  
    Sample x = new Sample(1,"Sagar");  
    Sample y = new Sample(1,"Hitesh");  
    //calling method to display the values of object  


1 Sagar
2 Hitesh

Constructor Overloading

Constructor overloading is a technique in Java that allows you to have multiple constructors with different parameter lists. They are organised in such a way that each constructor is responsible for a different task. The compiler distinguishes them based on the number of parameters in the list and their types.


class Student
   private int studentId;
   private String studentName;
   private int studentAge;
       //Default constructor
       studentId = 1;
       studentName = "Hitesh";
       studentAge = 10;
   Student(int num1, String str, int num2)
       //Parameterized constructor
       studentId = num1;
       studentName = str;
       studentAge = num2;
   //Getter and setter methods
   public int getStuID() {
       return stuID;
   public void setStuID(int studentId) {
       this.studentId = studentId;
   public String getStuName() {
       return studentName;
   public void setStuName(String studentName) {
       this.studentName = studentName;
   public int getStuAge() {
       return studentAge;
   public void setStuAge(int studentAge) {
       this.studentAge = studentAge;

   public static void main(String args[])
       //The default constructor would be called during the creation of this object.
       Student myobj = new Student();
       System.out.println("Student Name is: "+myobj.getStuName());
       System.out.println("Student Age is: "+myobj.getStuAge());
       System.out.println("Student Id is: "+myobj.getStuID());

       /*The parameterized would be called during the creation of this object.
        * constructor StudentData(int, String, int)*/
       Student myobj2 = new Student(22, "Sagar", 32);
       System.out.println("Student Name is: "+myobj2.getStuName());
       System.out.println("Student Age is: "+myobj2.getStuAge());
       System.out.println("Student Id is: "+myobj2.getStuID()); 


Student Name is: New Student
Student Age is: 10
Student Id is: 1
Student Name is: Sagar
Student Age is: 32
Student Id is: 22

Difference between method and constructor in Java

Java ConstructorJava Method
A constructor is used to initialize an object’s state.A method is used to expose an object’s behaviour.
A return type is not permitted in a constructorA return type is required for a method.
The constructor is called without being explicitly called.The method is called directly.
If a class requires a constructor, the Java compiler provides a default constructor.In any case, the compiler does not provide the method.

If you want to know more about method than refer to the Java Methods with Example.

Java Copy Constructor

In a Java class, a copy constructor creates an object by copying another object from the same Java class. This is useful when copying a complex object with multiple fields, or when making a deep copy of an existing object.


In this example, we’ll use the Java constructor to copy the values of one object to another.

class Student{  
    int id;  
    String name;  
    //constructor to initialize integer and string  
    Student(int i,String n){  
    id = i;  
    name = n;  
    //constructor to initialize another object  
    Student(Student s){  
    id =;  
    void display(){
    System.out.println(id+" "+name);
    public static void main(String args[]){  
    Student s1 = new Student(1,"Sagar");  
    Student s2 = new Student(s1);  
1 Sagar
1 Sagar

You might like:

Java Object and Java Class with Example

We hope that this article will assist you in understanding all about constructor in java. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.

This Post Has 3 Comments

Leave a Reply