What Is a Java Constructor

java constructor on macOne 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:

How do you invoke a constructor in Java? Easy:

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:

You can initialize it in the same way:

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:

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:

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:

and you want to add parametrized constructor with 2 arguments.

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

generate Java constructor using Intellij IDEA

Select fields and press “OK”.

Select fields to generate Java constructor using Intellij IDEA

The result is:

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:

It’s equivalent to:

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.

If I forgot something let me know in comments, please.

1 Star2 Stars3 Stars4 Stars5 Stars
Loading...
Scroll Up