Constructors in JAVA

Constructors in JAVA

In Java, a constructor is a block of codes similar to a method. It is called when an instance of the object is created.

Constructor is used to create an object of a class using the new keyword.

It is a special type of method which is used to initialize the object.

A constructor must have the same name as the class name and it cannot be static, abstract, final and synchronized.

In Java we have two types of constructors :-

  1. Default Constructor
  2. Parameterized Constructor 

Note:- A constructor has no return type

1. Default Constructor

A constructor that has no parameter is called a default constructor. If we do not define a constructor in a class, then compiler creates default constructor(with no arguments) for the class.

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

Program

Save as Codedost.java

class Codedost
{
    int number;
    String str;

    //this would be invoked while object
    //of that class created.
    Codedost()
    {
        System.out.println("Default Constructor Called!");
    }
    public static void main (String[] args)
    {
        //Calling the default constructor.
        Codedost cd = new Codedost();

        //Default constructor provides the default
        //values to the object like 0, null
        System.out.println(cd.number);
       System.out.println(cd.str);
    }
}

Output

Default Constructor Called!
0
null

2. Parameterized Constructor

A constructor that has parameters is known as parameterized constructor. The parameterized constructor is used to provide different or same values to the distinct objects.

Program

Save as Codedost.java

class Codedost
{
    String str;

    //this would be invoked while object
    //of that class created.
    Codedost(String s)
    {
        str = s;
    }

    void print()
    {
        System.out.println(str);
    }

    public static void main (String[] args)
    {
       //Calling the parameterized constructor.
       Codedost cd = new Codedost("Just Keep Coding!");
       cd.print();
    }
}

Output

Just Keep Coding!

Constructor Overloading

Like methods, we can overload constructors for creating objects in different ways. Compiler differentiates constructors on the basis of types of parameters, number of parameters and order of the parameters.

Below is the programming demonstrating constructor overloading

Program(Save as Codedost.java)

class Codedost
{
    //constructor with one argument
    Codedost(String name)
    {
       System.out.println("String = " +name);
    }

    //Constructor with one argument

    //With different datatype
    Codedost(int id)
    {
       System.out.println("Int = " +id);
    }

    //constructor with two arguments
    Codedost(String name,int age)
    {
        System.out.print("String = "+name+" Integer = "+age);
    }

    public static void main(String[] args)
    {
        //Creating objects of class named 'Codedost'

        //By passing different arguments
        Codedost c1 = new Codedost("Steve");
        //Invoke the constructor with one argument with different datatype
        Codedost c2 = new Codedost(21);
        //Invoke the constructor with two arguments
        Codedost c3= new Codedost("Elon", 43);
    }
}

Output

String = Steve
Int = 21
String = Elon Integer = 43

Difference between Constructors and Methods in Java

  1. Constructors must have the same name as the class within which it defined while it is not necessary for a method in java.
  2. Constructor is called only once at the time of Object creation while methods can be called multiple times.
  3. Constructors do not have any return type while a method has a return type or void if it does not return any value.
  4. Java compiler provides a default constructor, but the compiler does not provide any default method.
Share Me!
  •  
  •  
  •  
  •  
  •  
  •