Constructor in Java

constructor-in-java

Java Constructors

In this tutorial, we will learn about Java constructors, their types, and how to use them with the help of examples.

What is a Constructor?

A constructor in Java is similar to a method that is invoked when an object of the class is created.

Unlike Java methods, a constructor has the same name as that of the class and does not have any return type. For example,

class Test {
  Test() {
    // constructor body
  }
}

Here, Test() is a constructor. It has the same name as that of the class and doesn’t have a return type.

Example 1: Java Constructor

class Main {
  private String name;

  // constructor
  Main() {
    System.out.println("Constructor Called:");
    name = "Programiz";
  }

  public static void main(String[] args) {

    // constructor is invoked while
    // creating an object of the Main class
    Main obj = new Main();
    System.out.println("The name is " + obj.name);
  }
}

Output:

Constructor Called:
The name is Programiz

In the above model, we have made a constructor named Main(). Inside the constructor, we are introducing the estimation of the name variable.

Notice the statement of creating an object of the Main class.

Main obj = new Main();

Here, when the object is created, the Main() constructor is called. And, the value of the name variable is initialized.

Hence, the program prints the value of the name variables as Programiz.

Types of Constructor

In Java, constructors can be divided into 3 types:

  1. No-Arg Constructor
  2. Parameterized Constructor
  3. Default Constructor

1. Java No-Arg Constructors

Like techniques, a Java constructor might have any boundaries (contentions).

On the off chance that a constructor doesn’t acknowledge any boundaries, it is known as a no-contention constructor. For instance,

private Constructor() {
   // body of the constructor
}

Example 2: Java private no-arg constructor

class Main {

  int i;

  // constructor with no parameter
  private 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);
  }
}

Output:

Constructor is called
Value of i: 5

In the above model, we have made a constructor Main(). Here, the constructor doesn’t acknowledge any boundaries. Subsequently, it is known as a no-arg constructor.

Notice that we have announced the constructor as private.

When a constructor is proclaimed private, it can’t be gotten to from outside the class. Thus, making objects from outside the class is denied utilizing the private constructor.

Here, we are making the item inside a similar class. Thus, the program can get to the constructor. To find out additional, visit Java Implement Private Constructor.

In any case, in the event that we need to make objects outside the class, at that point we need to announce the constructor as open.

Example 3: Java public no-arg constructors

class Company {
  String name;

  // public constructor
  public Company() {
    name = "Programiz";
  }
}

public class Main {

  public static void main(String[] args) {

    // object is created in another class
    Company obj = new Company();
    System.out.println("Company name = " + obj.name);
  }
}

Output:

Company name = Programiz

2. Java Parameterized Constructor

A Java constructor can likewise acknowledge at least one boundaries. Such constructors are known as defined constructors (constructor with boundaries).

Example 4: Parameterized constructor

class Main {

  String languages;

  // constructor accepting single value
  Main(String lang) {
    languages = lang;
    System.out.println(languages + " Programming Language");
  }

  public static void main(String[] args) {

    // call constructor by passing a single value
    Main obj1 = new Main("Java");
    Main obj2 = new Main("Python");
    Main obj3 = new Main("C");
  }
}

Output:

Java Programming Language
Python Programming Language
C Programming Language

In the above example, we have created a constructor named Main(). Here, the constructor takes a single parameter. Notice the expression,

Main obj1 = new Main("Java");

Here, we are passing the single value to the constructor. Based on the argument passed, the language variable is initialized inside the constructor.

3. Java Default Constructor

On the off chance that we don’t make any constructor, the Java compiler consequently make a no-arg constructor during the execution of the program. This constructor is called default constructor.

Example 5: 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);
  }
}

Output:

a = 0
b = false

Here, we haven’t created any constructors. Hence, the Java compiler automatically creates the default constructor.

The default constructor initializes any uninitialized instance variables with default values.

TypeDefault Value
booleanfalse
byte0
short0
int0
long0L
char\u0000
float0.0f
double0.0d
objectReference null

In the above program, the variables a and b are initialized with default value 0 and false respectively.

The above program is equivalent to:

class Main {

  int a;
  boolean b;

  // a private constructor
  private Main() {
    a = 0;
    b = false;
  }

  public static void main(String[] args) {
    // call the constructor
    Main obj = new Main();

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

The output of the program is the same as Example 5.


Important Notes on Java Constructors

  • Constructors are conjured verifiably when you launch objects.
  • The two principles for making a constructor are:
  • The name of the constructor should be equivalent to the class.
  • A Java constructor should not have a bring type back.
  • On the off chance that a class doesn’t have a constructor, the Java compiler naturally makes a default constructor during run-time. The default constructor instates example factors with default esteems. For instance, the int variable will be instated to 0
  • Constructor types:
  • No-Arg Constructor – a constructor that doesn’t acknowledge any contentions
  • Defined constructor – a constructor that acknowledges contentions
  • Default Constructor – a constructor that is naturally made by the Java compiler in the event that it isn’t unequivocally characterized.
  • A constructor can’t be conceptual or static or last.
  • A constructor can be over-burden yet can not be superseded.

Constructors Overloading in Java

Similar to Java method overloading, we can also create two or more constructors with different parameters. This is called constructors overloading.

Example 6: Java Constructor Overloading

class Main {

  String language;

  // constructor with no parameter
  Main() {
    this.language = "Java";
  }

  // constructor with a single parameter
  Main(String language) {
    this.language = language;
  }

  public void getName() {
    System.out.println("Programming Langauage: " + this.language);
  }

  public static void main(String[] args) {

    // call constructor with no parameter
    Main obj1 = new Main();

    // call constructor with a single parameter
    Main obj2 = new Main("Python");

    obj1.getName();
    obj2.getName();
  }
}

Output:

Programming Language: Java
Programming Language: Python

In the above example, we have two constructors: Main() and Main(String language). Here, both the constructor initialize the value of the variable language with different values.

Based on the parameter passed during object creation, different constructors are called and different values are assigned.