Constructor in Java

A constructor is the special method of a class which is used to initialize the object at the time of object creation.

The constructor is a special method of a class because it has two important special properties.

1) Name of the Constructor and Name of the class in which it resides must be same and syntactically similar to a method.
2) Constructor does not have any return type not even void .


Important point:

  Java constructor is executed at the time of object creation.

  If we want to perform any operation at the time of Object creation then the constructor is the suitable place.

Types of java constructors

here are two types of constructors
1) Default Constructor : A non-argument constructor automatically created by JVM.
2) User-defined Constructor : Constructor created by Programmer/User.

1) Default Constructor

Zero -argument constructor : If we do not define any constructor in java class then java compiler creates one default constructor for class.

  Default constructor provides the default values to the object like 0, null etc. depending on the type.

example of the Default constructor .


//Create Student class
class Student {

    int id;
    String name;
    double marks;

    public static void main(String arr[]) {
        Student s = new Student();//creating..object
        //jvm call default constructor created by java compiler
        System.out.println("id = " + s.id);
        System.out.println("name = " + s.name);
        System.out.println("mark = " + s.marks);
    }
}
id = 0
name = null
mark = 0.0

Change done by Compiler in above example

  We have not defined any constructor in above example. so java compiler creates the default constructor for the class which initializes the object with the default values.

2) User-defined Constructor

User can create two types of constructor
1) Zero argument constructor: -which does not take any values as parameter . These constructors are also called the Non-Parameterized constructor.
2) Parametrized constructor :- which takes value in a parameter is called a parameterized constructor.

Example of a User-defined default constructor.


//Create Student class
class Student {

    int id;
    String name;
    double marks;

    Student() {
        System.out.println("This is user defined default constructor");
        id = 0;
        name = "";
        marks = 0.0;
    }

    public static void main(String arr[]) {
        Student s = new Student();//creating..object
       //now user defined constructor is called
        
        
        System.out.println("id = " + s.id);
        System.out.println("name = " + s.name);
        System.out.println("mark = " + s.marks);
    }
}
This is user defined default constructor
id = 0
name = 
mark = 0.0

Example of a parameterized constructor.


//Create Student class
class Student {

    int id;
    String name;
    double marks;

    Student(int x, String y, double  z) {
        System.out.println("This is paramaterized  constructor");
        id = x;
        name = y;
        marks = z;
    }

    public static void main(String arr[]) {
        Student s = new Student(44,"durgesh",44.5);//creating..object
       //now user defined parameterized constructor is called        
        
        System.out.println("id = " + s.id);
        System.out.println("name = " + s.name);
        System.out.println("mark = " + s.marks);
    }
}
This is paramaterized  constructor
id = 44
name = durgesh
mark = 44.5

Constructor Overloading in Java

The act of defining more than one constructor in a class is known as Costructor Overloading.

Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type

  Rule of constructor overloading : Number of argument, Type of argument or Order of argument at least one must be different in order to overload the constructor in a class.

//example of constructor overloading
class Student {

    int id;
    String name;
    double marks;

    public Student() {
        System.out.println("This is default constructor...");
    }

    Student(int x, String y, double z) {
        System.out.println("This is paramaterized  constructor");
        id = x;
        name = y;
        marks = z;
    }

    public void show() {

        System.out.println("id = " + id);
        System.out.println("name = " + name);
        System.out.println("mark = " + marks);
    }

    public static void main(String arr[]) {
        Student s = new Student(44, "durgesh", 44.5);//creating..object
        //now user defined parameterized constructor is called        
        s.show();
        //going to create another object of Student class
        Student s1 = new Student();
        s1.show();
    }
}
This is paramaterized  constructor
id = 44
name = durgesh
mark = 44.5
This is default constructor...
id = 0
name = null
mark = 0.0
woman avatar