Constructor in Java – Types of Constructor in Java

1. Objective

In this Java tutorial, we are going to study Constructor in Java, why we use Java constructor and various types of Constructor in Java: No-Argument (Default Constructor) & Parameterized Constructor in Java. Moreover, we will also discuss Constructor Overloading in Java with some Java Constructor example. At last, we discuss Difference Between Constructor and Method in Java.

Constructor in Java

The constructor in Java – Types of Constructor in Java

2. Constructor in Java

Java Constructors utilize to introduce the protest’s state. Like strategies, a constructor in Java additionally contains gathering of statements (i.e. guidelines) that execute at the time of Object creation.

a. When is a Constructor in Java called?

Each time a protest is made utilizing new() watchword no less than one constructor (it could be default constructor) is summoned to appoint beginning esteems to the information individuals from a similar class.

Let’s discuss How Java Inheritance is used

A constructor in Java is summoned at the season of protest or case creation.

Java Constructor Example –

class flair
{  
  .......
  new flair() {}
  .......
}

flair obj = new flair();

b. Rules for writing Constructor in Java

These are some rules for writing Java Constructor:

  1. Constructor(s) of a class must have the same name as the class name in which it lives.
  2. A Java constructor cannot be theoretical i.e. it should be abstract, last, static and Synchronized.
  3. Access modifiers can be utilized as a part of constructor statement to control its entrance i.e. which different class can call the constructor.

Read about Interface in Java with Example program

3. Types of Constructor in Java

There are two types of Java Constructors. Let’s discuss them one by one:

Types of Constructor in Java

Types of Constructor in Java

a. The No-Argument Constructor in Java

A Java constructor that has no parameter is call as Java default constructor. On the off chance that we don’t characterize a constructor in a class, at that point compiler makes default constructor (with no contentions) for the class. Furthermore, on the off chance that we compose a constructor with contentions or no-contention then the compiler does not make default constructor.

Default constructor in Java gives the default esteems to the protest like 0, invalid and so on relying upon the sort.

Example of Default Constructor in Java–

import java.io.*;
class Flair{
    int num;
    String name;
    Flair()
    {
        System.out.println("Constructor called");
    }
}
class GFG
{
    public static void main (String[] args)
    {
        Flair flair1 = new Flair();
        System.out.println(flair1.name);
        System.out.println(flair1.num);
    }
}

Let’s read about Serialization and Deserialization in Java

b. Parameterized Constructor in Java

A constructor that has parameters is call as a Java parameterized constructor. In the event that we need to introduce fields of the class with your own esteems, at that point utilize parameterized constructor.

Example of Parameterized Constructor in Java –

import java.io.*;
class Flair
{
    String name;
    int id;
    Flair(String name, int id)
    {
        this.name = name;
        this.id = id;
    }
}
class GFG
{
    public static void main (String[] args)
    {
        Flair flair1 = new Flair("adam", 1);
        System.out.println("FlairName :" + flair1.name +
                           " and FlairId :" + flair1.id);
    }
}

4. Constructor Overloading in Java

There is no “return value” articulations in a constructor, in Java. However, the constructor returns current class case. We can compose ‘return’ inside a constructor.

Read about Method overloading in Java with example in detail

Like strategies, we can over-burden constructors for making objects in various ways. Compiler separates constructors based on quantities of parameters, kinds of the parameters and request of the parameters.

Example –

import java.io.*;
class Flair
{
    Flair(String name)
    {
        System.out.println("Constructor with one " +
                      "argument - String : " + name);
    }
    Flair(String name, int age)
    {
        System.out.print("Constructor with two arguments : " +
                " String and Integer : " + name + " "+ age);
    }  
    Flair(long id)
    {
        System.out.println("Constructor with one argument : " +
                                            "Long : " + id);
    }
}
class GFG
{
    public static void main(String[] args)
    {
        Flair flair2 = new Flair("Shikhar");
        Flair flair3 = new Flair("Dharmesh", 26);
        Flair flair4 = new Flair(325614567);
    }
}

5. Difference Between Constructor and Method in Java

These are the following point of comparison between constructor vs method in Java.

  • While a Java constructor use to initialize the state of the object a method use to expose the behavior of the object.
  • A Java constructor must not have a return type but a method must always have a return type.
  • The invoking in a constructor is done implicitly while in a method it is done explicitly.
  • The compiler in Java provides a default constructor in case you don’t have one while there is no such case for methods.
  • The name of the constructor must be same as the class name while a method it is not a mandatory condition.

Read about Method Overloading vs Overriding in Java

This was all in Constructor in Java Tutorial. Hope you like our explanation.

6. Conclusion

Hence, in this tutorial for Java, we learned about constructors in Java and types of java constructor with examples. At last, we saw Constructor Overloading in Java. Furthermore, if you have any query feel free to ask in a comment section.

Related Topic- Garbage Collection in Java 

For reference 

1 Response

  1. abhi says:

    The English used in the tutorial is quite confusing and is not easy to understand. You guys should look to simplify the use of English, for example, this line

    “Default constructor in Java gives the default esteems to the protest like 0, invalid and so on relying upon the sort.”

    is not easy for a newbie(both for Java and English) to understand.

Leave a Reply

Your email address will not be published. Required fields are marked *