What Is a Java Constructor

One of the first thing that each beginner programmer should learn is a Java constructor.

In this article, I’ll explain what is a constructor in Java (default and parametrized) and different methods how to create a constructor in Java with examples.

I’ll show you how to overload constructors and how to access parent class constructor.

I’ve prepared small tricks how to generate constructor fast using Lombok Java library as well.

Keep reading and learn Java constructors…

Using Constructors in Java

What is the definition of a constructor in Java?

That means you define a way how to initialize a new object instance.

The purpose of the constructor is to initialize an object.

There are 2 types of constructor in Java:

  • Default constructor
  • Parametrized constructor

Default Java constructor syntax is pretty easy – it’s constructor without any parameters.

Example:

package com.explainjava;
 
public class Car {
    
    public Car() {
        
    }
}

How do you invoke a constructor in Java? Easy:

Car car = new Car();

I think you noticed that constructors should have the same name as a class.

Creating a parameterized constructor in Java is simple as well – just add some parameters.

Example:

package com.explainjava;
 
public class Car {
    
    private String color;
 
    public Car(String color) {
        this.color = color;
    }
}

You can initialize it in the same way:

Car car = new Car("red");

If your class doesn’t contain any parametrized constructors it’s not necessary to create Java default constructor.

What is a Difference Between Constructor and Method

  1. Constructors create new objects, methods perform operations on objects that already exist.
  2. You have to use new keyword to call constructor
  3. Constructors and methods look more or less similar in code. They can take parameters, they have access modifiers (like a public|protected|private), and they have method bodies in braces.
  4. Constructor should have the same name as a class, method can be named as you want
  5. The constructor can’t return anything, the method should return some object, primitive or void value.

Java Constructor Overloading

The best practice is to have a single primary constructor and refer it using this() with default parameters.

Java constructor overloading example:

public class Car {
 
    private String name;
    private String color;
 
    public Car(String name) {
        this(name, "red");
    }
 
    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }
}

As you can see I defined two parametrized constructors.

Constructor with a single parameter calls the 2nd constructor using this(name, "red") and provides “red” as a default color.

Looks like Java constructor chaining.
How To Access Super Class Constructor

Sometimes you need to access parent class constructor to initialize fields.

You can access it using super() call inside of child class constructor.

Example:

public class Car {
 
    private String name;
    private String color;
 
    public Car(String name) {
        this(name, "red");
    }
 
    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }
}
 
public class Bmw extends Car {
 
    public Bmw(String name) {
        super(name);
    }
}

As you can see I accessed Car’s constructor with 1 argument using super(name) from child class constructor.

If a superclass doesn’t have default constructor you have to call one of its parametrized constructors explicitly.

Can We Have Protected or Private Constructor in Java?

Yes, we can use access modifiers for Java constructor.

What does it mean?

Public constructor means everyone can call it.

Constructor with no modifier (package-private constructor) means it can be called inside of its own class and inside other classes in the same package.

Protected constructor means you can access it in its own class, its subclass, and other classes within the same package.

Private constructor means it can be called inside of its own class only. It useful for singleton pattern.

Destructor in Java

Unlike C/C++ Java doesn’t have a destructor.

It’s not necessary because Garbage Collector cares about memory cleaning.

if there are no references to the object GC removes it (GC works in a more complicated way, of course, I’ll explain it in one of the future articles).

Tips & Tricks

I should be honest with you – I’m a lazy developer.

I’m looking for ways how to avoid writing boilerplate code like Java constructors.

I prepared 2 small tips how to write constructors faster.

Generate Constructors Using Intellij IDEA

For example, you have a class:

package com.explainjava;
 
public class Car {
 
    private String name;
    private String color;
 
}

and you want to add parametrized constructor with 2 arguments.

Right click on class – “Generate…” (Alt + Insert) and select “Constructor”.

Select fields and press “OK”.

The result is:

package com.explainjava;
 
public class Car {
 
    private String name;
    private String color;
 
    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }
}

Simple and fast. Use it.

Lombok

Lombok is an awesome library.

It’s one of the best things in Java world.

Just take a look how can be easily constructor added:

package com.explainjava;
 
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
 
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
public class Car {
 
    private String name;
    private String color;
}

It’s equivalent to:

package com.explainjava;
 
public class Car {
 
    private String name;
    private String color;
 
    private Car() {
 
    }
 
    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }
}

As for me, 1st version looks really better and cleaner.

You can read more about Lombok project here.

Conclusion

I hope my article about Java constructors was good for you.

I tried to collect all necessary information about it.

Leave a Comment