Introduction To Constructor In Java

Introduction

In this article we discuss constructors in Java. Also discuss their types, rules, etcetera.

Constructor

A constructor it is a special kind of method; it defines the state of an object (initializes an object). When the object is created then by default a constructor is generated by the Java compiler.

A constructor is just like another instance method but it does not have any explicit return type. It develops the values (data) for the object, that is why it is known as a constructor.

Some rules for creating constructor

The following are some rules for constructors:

  1. It has no explicit return type.
  2. The name of the constructor must be the same as the class name.

Types

Constructors are basically one of the following three types:

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

1. Default Constructor

A Default Constructor is also called an empty constructor. A constructor with no parameter is known as a Default Constructor.

Syntax

[class_name](){ }

Example

class Car
  {
    Car()
      {
        System.out.println("Car is ready to drive");
      }
   
public static void main(String args[])
      {
        Car c= new Car();
      }
  }

Output

pic-1.jpg

Note: If we don't create a constructor in our program then the compiler automatically generates a default constructor for the class.

2. Parameterized Constructor

A Parameterized Constructor is another type of constructor. It has parameter (arguments) so it is called a Parameterized Constructor.

It provides different values to the different objects.

Example

class Childrens1
  {
    int roll_no;
    String stname;
    Childrens(int i, String nm)
     
{
       
roll_no=i;
       
stname=nm;
      }
   
void display()
     
{
       
System.out.println(roll_no + " " + stname);
     
}
   
public static void main(String args[])
     
{
       
Childrens1 ch1 = new Childrens1(110, "Rahul Pandey");
       
Childrens1 ch2= new Childrens1(220, "Shridher Kaushik");
       
Childrens1 ch3= new Childrens1(330, "Rahman Malik");
       
Childrens1 ch4= new Childrens1(440, "Rahul Khanna");
       
ch1.display();
       
ch2.display();
       
ch3.display();
       
ch4.display();
      }
  }

Output

pic-2.jpg

3. Copy Constructor

In this example, the values of an object is copied into another object using a Copy Constructor.

class Childrens1
 
{
    int roll_no
;
    String stname
;
    Childrens1(int i, String nm
)
     
{
        roll_no=i
;
        stname=nm
;
     
}
    Childrens1(Childrens1 ch
)
    
  {
        roll_no=ch.roll_no
;
        stname=ch.stname
;
     
}
    void display
()
     
{
        System.out.println(roll_no + " " + stname
);
     
}
    public static void main(String args[]
)
     
{
        Childrens1 ch1 = new Childrens1(110, "Rahul Pandey"
);
        Childrens1 ch2= new Childrens1(ch1
);
        ch1.display
();
        ch2.display
();
      
}
 
}

 Output

pic-4.jpg

Overloading of constructors

In this technique a class can have any number of contructors but they all are different in their parameter lists. The compiler differentiates them by their parameter lists.

Example

class Childrens
 
{
   
int roll_no;
   
String stname;
   
int age;
   
Childrens(int i, String nm)
     
{
       
roll_no=i;
       
stname=nm;
      }
   
Childrens(int i, String nm, int a)
     
{
       
roll_no=i;
       
stname=nm;
       
age=a;
      }
   
void display()
     
{
       
System.out.println(roll_no+" " +stname+ " " + age);
     
}
   
public static void main(String args[])
     
{
       
Childrens ch1 = new Childrens(111, "Rahul Pandey");
       
Childrens ch2= new Childrens(222, "Shridher Kaushik", 30);
       
ch1.display();
       
ch2.display();
      }
  }

Output

pic-3.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all