Everything you need to know about constructors in Java





Hey guys in this post we will discuss constructors in java with examples

Overview


Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements that are executed at time of object creation.

Syntax


Following is the syntax for creating constructors in java

public class ClassName {
    
    ClassName () {
        
    }
}

Need of constructors


So constructors are used to assign values to the class variables at the time of object creation, either explicitly done by the programmer or by java itself.

When is a constructor called?


Each time an object is created using new() keyword at least one constructor is invoked to assign initial values to the data members of the same class.

Rules for creating constructor


There are 2 rules defined for the constructor.

  1. Constructor name must be the same as its class name
  2. A constructor must have no explicit return type
  3. A java constructor cannot be abstract, static, final, and synchronized.

Types of constructor


There are 2 types of constructors in java

  1. Default constructor (no-arg constructor)
  2. Parameterized constructor

Default constructor (no-arg constructor)


A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates a default constructor (with no arguments) for the class.




Default constructor provides the default values to the object like 0, null, etc. depending on the type.

public class Test {
    
    public Test() {
        System.out.println("This is no-arg constructor");
    }
    public static void main(String args[]) {
        new Test();
    }
}

Output:

This is no-arg constructor

Parameterized constructor


A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.

public class Test {
    String name;
    
    //parameterized constructor
    public Test(String name) {
        this.name = name;
    }
    
    public void printData() {
        System.out.println("Name is "+name);    
    }
    
    public static void main(String args[]) {
        new Test("B2 Tech").printData();
    }
}

Output:

Name is B2 Tech

Does constructor return any value?


There are no “return value” statements in constructor, but constructor returns current class instance. We can write “return” inside a constructor.

How constructors are different from methods in Java?


  • Constructors must have the same name as the class within which it defined while it is not necessary for the method in java.
  • Constructors do not return any type while methods have the return type or void if does not return any value.
  • Constructor is called only once at the time of object creation while methods can be called any number of time.

That’s all about Constructors in java. I hope you enjoyed this post, if you find this post is useful, please share it with your friends/collegues or in any social media. Thank you.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply