Constructor in Java

Table of Contents

Constructor in Java

One of the most important characteristics of Java is the Constructors facility which is provided by it. Even there is also a definition of Constructor Class which can be found in the java.lang.reflect package, which is used to get the overall internal information of a Constructor within a class.

What is Constructor?

As the name suggests, Constructor is used for initializing an object of a class. It is called every time a new object of a class is created using new() keyword and memory is allocated to the same with some default value, but if the programmer wants to provide it with some initial value then it’s up to the programmer that which value they want to store.

Now let’s see the rules of writing a Constructor.

Rules for creating Java constructor

There are multiple rules defined for the constructor.

  • Name of the constructor should be the same as the name of the class
  • It must have no explicit return type, although you cannot use return type as it returns a specific value.
  • It cannot be abstract, static, final, and synchronized
  • Even Access Modifiers can be used to restrict the scope of it, like public, private, and protected.

Types Of Constructor

There are two types of Constructor, which can be seen to be used while writing code in Java, those are:

  • Default Constructor
    • Now, how does a default constructor looks like and what is the use of it, why is it created?
      • So, whenever there is no parameter provided to the object created of the class, then this Default Constructor is called.
      • Just have a look at the following code.
class Sample
   {  
      Sample()//This is the Default Constructor that is created
      {
        System.out.println("Constructor executed");
      }  
    public static void main(String args[])
    {  
      Sample a=new Sample();  
     }  
}  

So, this Default Constructor will be automatically created as soon as we create an object of the class, by saying that, what do I mean that, you don’t need to create a Constructor of it.

  • Parameterized Constructor
    • As the name suggests, it must need parameters to be executed, but you might ask a question that why we would do that? And the answer is that, whenever we need an object to be initialized with a distinct value, then we can send the value as a parameter to it.
    • Let’s understand, how to create a Parameterized Constructor with an example.
class Sample
{  
    int a = 5;  
    Sample(int i)//Parametrizeed Constructor is created
    {  
      a = i;  //Vlaue of a updated with the value of i
    }  
    void display()
    { 
      System.out.println(a+" ");
    } 
    public static void main(String args[])
    {  
      Sample c = new Sample(10);  //An object is created 
      c.display();   
   }  
}  

Hope the concept is clear till here, now let’s understand the difference between a constructor an a method, as constructor also looks like a method.

Difference between Constructor and Method

  • The constructor must have a name similar to the class name of it, but for a method, it is not necessary to be always the same.
  • A constructor is invoked implicitly while a method is invoked explicitly.
  • JVM automatically creates a Constructor even if you haven’t done it specifically but it is not for the methods.
  • A method must have a return type but a constructor must not have a return type.
  • And the method is created to perform a certain task while the constructor is used to initializing the object created

Leave a Reply