Java Constructors

In this article, you’ll learn about Java constructors, how to create and use constructors with the help of examples.

Constructor

  • A constructor is a member function of a class with the same name as its class and has no return type.
  • A constructor for a class is a special method that is used to initialize a newly created object of that class type with a legal initial value.
  • It is called implicitly, just after the memory is allocated for the object.
  • It is not mandatory for the programmer to write a constructor for a class.
  • When there is no constructor defined in the class by the programmer, compiler implicitly provides a default constructor for the class.

Defining Constructor

There are two ways of defining a constructor:

  • Without Arguments
  • With Arguments

Constructor Without Arguments

A constructor that accepts no parameter is called default constructor.

If we do not define a constructor in a class, then the compiler will automatically create a default constructor (with no arguments) during runtime for the class, with data members which have values like zero, null, etc.

But, if we define a constructor in a class with arguments or no arguments, then the compiler does not create a default constructor.

Syntax:

The syntax of no-argument constructor is:

accessModifier ClassName() {
    // constructor body
}

 

Example: Defining no-argument Constructor in a class

class Student {
    int rollNo;
    String name;

    public Student() {
        rollNo = 5;
        name = "Arun Verma";
    }
}
 class Person {
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println("Roll no :" + student.rollNo + "\n" + "Name :" + student.name);
    }
}

Output:

Roll no :5
Name :Arun Verma

Note: To initialize the state of an object with some value we have to write the initialization statement inside the constructor otherwise the data members will initialize with the default datatype values like zero, null, etc.

 

Example: Not defining any Constructor in a class

class Student {
    int rollNo;
    String name;
}

class Person {
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println("Roll no :" + student.rollNo + "\n" + "Name :" + student.name);
    }
}

The above program is equivalent to:

class Student {
    int rollNo;
    String name;
   
   //default constructor created by the compiler
    public Student() {
    }
}

class Person {
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println("Roll no :" + student.rollNo + "\n" + "Name :" + student.name);
    }
}

Output:

Roll no :0
Name :null

Constructor With Arguments

A constructor which consist of arguments is called a parameterized constructor.

It is used to assign values to distinct objects of that class type.

Syntax:

The syntax of parameterized constructor is:

accessModifier ClassName(arg1,arg2,....,argn) {
    // constructor body
}

 

Example: Constructor with arguments

class Student {
    int rollNo;
    String name;

    public Student(int roll_no, String student_name) {
        rollNo = roll_no;
        name = student_name;
    }

    void show() {
        System.out.println(rollNo + " : " + name);
    }

}
class Person {

    public static void main(String[] args) {
        Student s1 = new Student(101, "Arun Verma");
        Student s2 = new Student(102, "Rohit Sharma");
        Student s3 = new Student(103, "Pooja Singh");
        
        s1.show();
        s2.show(); 
        s3.show();       
    }
}

Output:

101 : Arun Verma
102 : Rohit Sharma
103 : Pooja Singh

Constructor Overloading

Just like method overloading, constructors can be overloaded to create objects in different ways.

The compiler differentiates constructors based on no. of arguments present in the constructor and other parameters like the order in which the arguments are passed.

Example: Constructor Overloading

class Student {
    int rollNo;
    String name;
    String subject;
   
   //no-argument constructor
    public Student() {
        rollNo = 101;
        name = "Arun Verma";
        subject = "Chemistry";
    }
   
    //constructor with two parameters
    public Student(int roll_no, String student_name) {
        rollNo = roll_no;
        name = student_name;
        subject = "Physics";
    }
   //constructor with three parameters
    public Student(int roll_no, String student_name, String subject_name) {
        rollNo = roll_no;
        name = student_name;
        subject = subject_name;
    }

    void show() {
        System.out.println(rollNo + " : " + name + " : " + subject);
    }

}

class Person {
    public static void main(String[] args) {
       //no-argument constructor called
        Student s1 = new Student();
       //constructor(with two parameters) called
        Student s2 = new Student(102, "Rohit Sharma");
       //constructor(with three parameters) called
        Student s3 = new Student(103, "Sandhya Singh", "Biology");

        s1.show();
        s2.show();
        s3.show();
    }
}

Output:

101 : Arun Verma : Chemistry
102 : Rohit Sharma : Physics
103 : Sandhya Singh : Biology

How constructor different from methods

  • A Constructor is used to initialize the state of an object whereas a method is used to expose the behavior (functionality) of an object.
  • Constructor must have the same name as the class within which it defined while it is not mandatory for the method in java.
  • Constructor has no return-type while methods have return-type or void if it does not return any value.
  • Constructor is called only once at the time of Object creation while method can be called any numbers of time.

Leave a Reply