Constructor in Java

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.

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

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.

To get in-Depth knowledge on Java you can enroll for a live demo on Java Online Training

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.

Example-

package com.OnlineITGuru.constructor;
class OnlineITGuru
{
  int number;
  String name;
  OnlineITGuru()
  {
    System.out.println("Default Constructor called");
  }
}
public class DefaultConstructor
{
  public static void main(String[] args)
  {
    OnlineITGuru objectName = new OnlineITGuru();
    System.out.println(objectName.name);
    System.out.println(objectName.number);
  }
}

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.

Take your career to new heights of success with Java certification course

Example-

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

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.

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.

To learn more about Constructors in Java and other great features of Java, you can enroll for a live demo on Java Training

Example –

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

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.

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.

Published by sindhuja cynixit

i am working as a digital marketing executive

Leave a comment

Design a site like this with WordPress.com
Get started