Beginner friendly series – Understanding Java Constructors

Article directory

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

What is a constructor?

Constructors in Java are similar to methods that are called when an object of a class is created.
    Unlike Java methods, constructors have the same name as the class and do not have any return type. E.g,

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

Here, Test() is a constructor. It has the same name as the class and has no 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 example, we created a constructor called Main(). Inside the constructor, we are initializing the name variable.
    Note the statement that creates an object of the Main class:

Main obj = new Main();

Here, when the object is created, the Main() constructor will be called. And, the value of the name variable is initialized.
    So the program prints the value of the name variable as Programiz.

type of constructor

In Java, constructors can be divided into 3 types:
    1. No parameter constructor
    2. Parameterized constructor
    3. Default constructor

1. Java parameterless constructor

Similar to methods, Java constructors may or may not have any parameters.
    If the constructor does not accept any parameters, it is called a parameterless constructor. E.g,

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

Example 2: Java private parameterless 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 example, we created a constructor Main(). Here, the constructor does not accept any parameters. Hence, it is called a parameterless constructor.
    Note that we have declared the constructor as private.
    Once the constructor is declared private, it cannot be accessed from outside the class. Therefore, using private constructors to create objects from outside the class is prohibited.
    Here, we create objects in the same class. Therefore, the program is able to access the constructor.
    However, if you want to create an object outside the class, you need to declare the constructor as public.

Example 3: Java public parameterless constructor

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

Java constructors can also accept one or more parameters. Such constructors are called parameterized constructors (constructors with parameters).

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 created a constructor called Main(). Here, the constructor takes a single parameter. Note the expression:

Main obj1 = new Main("Java");

Here we pass a single value to the constructor. The language variable is initialized inside the constructor according to the passed parameters.

3. Java default constructor

If we don’t create any constructor, the Java compiler automatically creates a parameterless constructor during program execution. This constructor is called the 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 are not creating any constructors. Therefore, the Java compiler automatically creates a default constructor.
    The default constructor initializes all uninitialized instance variables with default values.

Type Default Value
boolean false
byte 0
short 0
int 0
long 0L
char \u0000
float 0.0f
double 0.0d
object Reference null

In the above program, variables a and b are initialized with default values ​​of 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 this program is the same as Example 5.

Important note about Java constructors

  • When an object is instantiated, the constructor is implicitly called.
  • The two rules for creating constructors are:
    • The name of the constructor should be the same as the class.
    • Java constructors must not have a return type.
  • If the class has no constructor, the Java compiler automatically creates a default constructor at runtime. The default constructor initializes instance variables with default values. For example, an int variable will be initialized to 0.
  • Constructor type:
    • no-argument constructor – a constructor that takes no arguments
    • parameterized constructor – a constructor that accepts parameters
    • Default Constructor – A constructor automatically created by the Java compiler if not explicitly defined.
  • Constructors cannot be abstract or static or final.
  • Constructors can be overloaded, but not overridden.

Constructor overloading in Java

Similar to Java method overloading, we can also create two or more constructors with different parameters. This is called constructor 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();
  }
}

In the above example, we have two constructors: Main() and Main(String language). Here, both constructors initialize the value of the variable language with different values.
    Depending on the parameters passed during object creation, different constructors are called and different values ​​are assigned.
    A constructor can also be called from another constructor. To learn more, visit [Java Call One Constructor from Another] .
    Note: We use the this keyword to specify class variables. To learn more about this keyword, visit Java this keyword .

Leave a Comment

Your email address will not be published. Required fields are marked *