Constructor in Java – Master all the Concepts in One Shot!

Constructor in Java is an object builder or a block of code which creates an object. It is very similar to a Java method the main difference is that it does not have a return type, not even void. It is often referred to as a method. Constructor invoked automatically when an object is created.

NoteWhen an object is created at least one constructor is called. If there is no constructor then the default constructor is called.

Java Constructor with Example

Rules for Writing Constructors

These are some rules for writing constructors in Java:

  • The name of the constructor must be the same as the class name.
  • A constructor must have no return type not even void.
  • In constructor access modifiers are used to control its access so that other classes can call the constructor.
  • A constructor cannot be final, abstract, abstract and synchronized.

It’s time to revise the concept of Classes and Objects in Java

Types of Constructor in Java

Following are the two types of constructors, let’s discuss them with examples:

1. Default Constructor

A constructor that has no parameter is called as a default constructor. If we do not implement any constructor in our program then Java compiler automatically creates a constructor.

Default Constructor in Java

Example-

package com.dataflair.constructor;
class DataFlair
{
  int number;
  String name;
  DataFlair()
  {
    System.out.println("Default Constructor called");
  }
}
public class DefaultConstructor

{
  public static void main(String[] args)
  {
    DataFlair objectName = new DataFlair();
    System.out.println(objectName.name);
    System.out.println(objectName.number);
  }
}

Output-Default-constructor-program

2. Parameterized Constructor

A constructor that has parameters is called a parameterized constructor. It is mainly used to provide different values to distinct objects. Although you can use the same values too.

Example-

package com.dataflair.constructor;
class DataFlair
{
  String employeeName;
  int employeeId;
  DataFlair(String employeeName, int employeeId)
  {
    this.employeeName = employeeName;
    this.employeeId = employeeId;
  }
}
public class ParameterizedConstructor {
  public static void main (String[] args)
  {
    DataFlair employeeObject = new DataFlair("Renuka", 20);
    System.out.println("DataFlarian Name " + employeeObject.employeeName +
        " and DataFlarian Id :" + employeeObject.employeeId);
  }
}

Output-parameterized-constructor

Java Constructor Chaining

When we call a constructor from another constructor of the same class then the procedure is known as constructor chaining in Java. The main purpose of constructor chaining is that you can pass parameters through different constructors, but the initialization is done at the same place. If two different constructors require the same parameter then unnecessary we need to initialize that parameter twice.

Java Quiz

Constructor Overloading in Java

Overloading is basically having multiple instances of the same thing. Constructor Overloading in java simply means that having more than one constructor but with different parameter lists. So that the multiple constructors perform different tasks. Like methods, we can overload constructors.

Don’t forget to check Constructor Overloading in Java

Example –

package com.dataflair.constructor;
class DataFlair
{
  DataFlair(String name)
  {
    System.out.println("Constructor with one " +
        "argument - String : " + name);
  }
  DataFlair(String name, int age)
  {
    System.out.print("Constructor with two arguments - String and Integer : " + name + " "+ age);
  } 
  DataFlair(long id)
  {
    System.out.println();
    System.out.println("Constructor with one argument - Long : " + id);
  }
}
public class ConstructorOverloading {  
  public static void main(String[] args)
 {
  DataFlair ObjectName = new DataFlair("Renuka");
  DataFlair ObjectName1 = new DataFlair("Aakash", 26);
  DataFlair ObjectName2 = new DataFlair(325614567);
 }
}

Output-constructor-overloading

Difference Between Constructor and Method in Java

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

  1. The constructor is a block of code which instantiates a newly created object. On the other hand, a method is a collection of statements that always return value depends upon its execution.
  2. Constructors are called implicitly, while methods are called explicitly.
  3. A constructor does not have any return type but methods may have.
  4. The name of the constructor should be the same as the class name. On the other hand, the Method name should not be the same as the class name.
  5. If there is no constructor then the default constructor is created by the compiler itself. In the case of the method, there is no default method provided.

Difference Between Constructor and Method in Java

Summary

From the above tutorial, we can say constructors are more important than methods in Java. We can use it to initialize the objects to default values at the time of object creation. Constructors are not mandatory, but if you create them they should follow all the protocols.

It’s right time to know about Copy Constructor in Java

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 *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.