Constructors in Java

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.

It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.

Example

Create a constructor:


 class Main {
  int x;  
   Main() {
    x = 5;  
  }

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println(myObj.x); 
  }
}

Different from Constructors and Methods in Java? 

  • Constructors must have the same name as the class within which it is defined while it is not necessary for the method in Java.
  • Constructors do not return any type while method(s) have the return type or void if does not return any value.
  • Constructors are called only once at the time of Object creation while method(s) can be called any number of times.

The rules for writing constructors are as follows.

  • Constructor(s) of a class must have the same name as the class name in which it resides.
  • A constructor in Java can not be abstract, final, static, or Synchronized.
  • Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.

Types of Constructor

In Java, constructors can be divided into 3 types:

Non -Parameterized Constructor

Parameterized Constructor

Default Constructor

Non -Parameterized Constructor

Similar to methods, a Java constructor may or may not have any parameters (arguments).

If a constructor does not accept any parameters, it is known as a no-argument constructor. For example,

class Main {

int i;

// constructor with no parameter
Main() {
i = 5;
System.out.println(“Constructor is called”);
}

public static void main(String[] args) {

// calling the constructor without any parameter
Main obj = new Main();
System.out.println("Value of i: " + obj.i);

}
}

Parameterized Constructor

A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructor with parameters).

Example : Parameterized constructor

class Main {

  String languages;

  Main(String lang) {
    languages = lang;
    System.out.println(languages + " Programming Language");
  }

  public static void main(String[] args) {

    Main obj1 = new Main("Java");
    Main obj2 = new Main("Python");
    Main obj3 = new Main("C");
  }
}

Default Constructor

If we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program. This constructor is called default constructor.

Example : Default Constructor

class Main {

  int a;
  boolean b;

  public static void main(String[] args) {

    // A default constructor is called
    Main obj = new Main();

    System.out.println("Default Value:");
    System.out.println("a = " + obj.a);
    System.out.println("b = " + obj.b);
  }
}

Super() 

Whenever a child class constructor gets/is invoked, it implicitly invokes the constructor of the parent class. You can say that the compiler inserts a super(); statement at the beginning of the child class constructor.

Constructor overloading in Java

In Java, we can overload constructors like methods. The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.

class Demo{
      int  value1;
      int  value2;
      /*Demo(){
       value1 = 10;
       value2 = 20;
       System.out.println("Inside 1st Constructor");
     }*/
     Demo(int a){
      value1 = a;
      System.out.println("Inside 2nd Constructor");
    }
    Demo(int a,int b){
    value1 = a;
    value2 = b;
    System.out.println("Inside 3rd Constructor");
   }
   public void display(){
      System.out.println("Value1 === "+value1);
      System.out.println("Value2 === "+value2);
  }
  public static void main(String args[]){
    Demo d1 = new Demo();
    Demo d2 = new Demo(30);
    Demo d3 = new Demo(30,40);
    d1.display();
    d2.display();
    d3.display();
 }
}

Reference

https://www.javatpoint.com/java-constructor

https://www.programiz.com/java-programming/constructors

https://www.geeksforgeeks.org/constructors-in-java/

https://beginnersbook.com/2013/03/constructors-in-java/

Leave a comment

Design a site like this with WordPress.com
Get started