Back Button
Back to Chapter
Introduction
No items found.
Collapse_Icon
Table of Contents

While performing some logic functions, the constructor will be executed first before accessing other variables or functions. A constructor in Java is similar to a method that is invoked when an object of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory.

Rules for creating Java constructor

There are three rules defined for the constructor.

  1. Constructor name must be the same as its class name
  2. A Constructor must have no return type
  3. A Java constructor cannot be static. We know static keyword belongs to a class rather than the object of a class. A constructor is called when an object of a class is created, so no use of the static constructor.

Types of Java constructors

There are three types of constructors in Java:

  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor

1. Default Constructor:

The term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors. A constructor is called "Default Constructor" when it doesn't have any parameter.

syntax:

class_name(){}

Example:

class Student {
   //creating a default constructor  
   Student() {
       System.out.println("Welcome to ShapeAI and This is Constructor");
   }
   //main method  
   public static void main(String args[]) {
       //calling a default constructor  
       Student s1 = new Student();
   }
}

/*OUTPUT:
Welcome to ShapeAI and This is Constructor */

2. Parameterized Constructor:

A constructor which has a specific number of parameters is called a parameterized constructor. The parameterized constructor is used to provide different values to distinct objects.

syntax:

class_name(parameter-list){}

Example:

public class Student {
   String name;
   int age;
   //constructor
   Student(String n, int a) { // parameterized constructor with parameters
       this.name= n;
       this.age = a;
   }
   void display() {
       System.out.println(name + " " + age);
   }
   public static void main(String args[]) {
       Student s1 = new Student("Anu", 20);
       s1.display();
   }
}

/*OUTPUT:
Anu 20 */

3. Copy Constructor:

A copy constructor is a constructor that creates a new object using an existing object of the same class. It returns a duplicate copy of an existing object of the class.

syntax:

Class_name(Class_name object_name){ }

Example:

public class Student {
   int id;
   String name;
   //constructor to initialize integer and string  
   Student(int i, String n) {
       this.id = i;
       this.name = n;
   }
   //constructor to initialize another object  
   Student(Student s) {
       this.id = s.id;
       this.name = s.name;
   }
   void display() {
       System.out.println(id + " " + name);
   }

   public static void main(String args[]) {
       Student s1 = new Student(101, "Anu");
       Student s2 = new Student(s1);
       s1.display();
       s2.display();
   }
}
/*OUTPUT:
101 Anu
101 Anu */