Sitemap
Kamran Babayev

Kamran Babayev

Willing to learn new technologes, improve technical skills and apply them in software development.

Java Constructors

7 min readJun 25, 2023

In this story, 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 example, we have created a constructor named Main(). Inside the constructor, we are initializing the value 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

Java No-Arg Constructors
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
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 have created a constructor Main(). Here, the constructor does not accept any parameters. Hence, it is known as a no-arg constructor.

Once a constructor is declared private, it cannot be accessed from outside the class. So, creating objects from outside the class is prohibited using the private constructor.

Here, we are creating the object inside the same class. Hence, the program is able to access the constructor. However, if we want to create objects outside the class, then we need to declare the constructor as public.

class Company {
String name;

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

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

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

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.

Java 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.

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

Default Value:
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.

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

Get Kamran Babayev’s stories in your inbox

Join Medium for free to get updates from this writer.

The above program is equivalent to:

class Main {

int a;
boolean b;

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);
}
}

Output

Default Value:
a = 0
b = false

Important Notes on Java Constructors

  • Constructors are invoked implicitly when you instantiate objects.
  • The two rules for creating a constructor are:
    The name of the constructor should be the same as the class.
    A Java constructor must not have a return type.
  • If a class doesn’t have a constructor, the Java compiler automatically creates a default constructor during run-time. The default constructor initializes instance variables with default values. For example, the int variable will be initialized to 0
  • Constructor types:
    No-Arg Constructor — a constructor that does not accept any arguments
    Parameterized constructor — a constructor that accepts arguments
    Default Constructor — a constructor that is automatically created by the Java compiler if it is not explicitly defined.
  • A constructor cannot be abstract or static or final.
  • A constructor can be overloaded but can not be overridden.

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.

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.

It is also possible to call one constructor from another constructor.

Java program to call one constructor from another

class Main {

int sum;

// first constructor
Main() {
// calling the second constructor
this(5, 2);
}

// second constructor
Main(int arg1, int arg2) {
// add two value
this.sum = arg1 + arg2;
}

void display() {
System.out.println("Sum is: " + sum);
}

// main class
public static void main(String[] args) {

// call the first constructor
Main obj = new Main();

// call display method
obj.display();
}
}

Output

Sum is: 7

In the above example, we have created a class named Main. Here, you have created two constructors inside the Main class.

Main() {..}

Main(int arg1, int arg2) {...}

Inside the first constructor, we have used this keyword to call the second constructor.

Note: The line inside a constructor that calls another constructor should be the first line of the constructor. That is, this(5, 2) should be the first line of Main().

Call the constructor of the superclass from the constructor of the child class

We can also call the constructor of the superclass from the constructor of child class using super().

// superclass
class Languages {

// constructor of the superclass
Languages(int version1, int version2) {

if (version1 > version2) {
System.out.println("The latest version is: " + version1);
}

else {
System.out.println("The latest version is: " + version2);
}

}
}

// child class
class Main extends Languages {

// constructor of the child class
Main() {
// calling the constructor of super class
super(11, 8);
}

// main method
public static void main(String[] args) {

// call the first constructor
Main obj = new Main();

}
}

Output

The latest version is: 11

In the above example, we have created a superclass named Languages and a subclass Main. Inside the constructor of the Main class, notice the line:

super(11, 8);

Here, we are calling the constructor of the superclass (i.e. Languages(int version1, int version2)) from the constructor of the subclass (Main()).

Let us do one test related to constructor:

What is the output of the following?

package beach;
public class Sand {
public Sand() {
System.out.print("a");
}
public void Sand() {
System.out.print("b");
}
public void run() {
new Sand();
Sand();
}
public static void main(String... args) {
new Sand().run();
}
}

A. a
B. ab
C. aab
D. None of the above

C. The main() method calls the constructor which outputs a. Then the main method calls the run() method. The run() method calls the constructor again, which outputs a again. Then the run() method calls the Sand() method, which happens to have the same name as the constructor. This outputs b. Therefore, Option C is correct.

--

--

Kamran Babayev
Kamran Babayev

Written by Kamran Babayev

Willing to learn new technologes, improve technical skills and apply them in software development.