Default Constructor in Java

Video Tutorial
FREE
Constructor thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

Overview

A default constructor in Java is created by the compiler itself when the programmer doesn't create any constructor. The purpose of the default constructor is to initialize the attributes of the object with their default values.

What is Default Constructor in Java?

A default constructor is created only when we don't declare any constructor in our code. Then, by default, the compiler automatically creates a default constructor.

Since we know the role of the constructor in Java, the question is, what will happen when there is no constructor available during object creation?

But firstly, we need to look at an important case where we try to access any variable without initialization. Here in this example, we'll be simply declaring the variables but won't initialize them with their default values.

Compilation error:

And indeed, this is what you might have expected. The above code has thrown multiple errors. Why? Because we didn't bother to initialize the variables with their default values. Also, these variables are declared within the static method, so these are static variables. It is important to provide static variables with their default values.

Now, we'll provide default values for them.

Output:

Now, the code is working properly. So, the above example highlighted a common mistake that we, as programmers, might do while creating objects as well.

Instance variables are the ones declared as a property of a class but outside of constructors, methods, or blocks of the class. Instance Variables

So, initializing the instance variables is also mandatory. Here comes the role of a default constructor in Java that assigns these instance variables with their default values.

Note: We usually call the default constructor a no-arg constructor but they are not actually the same. A no-arg constructor is still a constructor created by the user and not by the compiler. Although they both have the same purpose, one is created automatically while the other is created by the user.

Default Constructor Example

Let us take an example of a product that comprises data such as id, product_name, and also price.

Notice what happens when no constructor is declared in the code by a programmer, and still, we are trying to access the instance variables.

Output:

It works! The above code didn't throw any error because the Java compiler provided a default constructor for the code to initialize the attributes with default values. Yes, this default constructor is invisible.

Purpose of a Default Constructor

As discussed above, the purpose of the default constructor is to provide the default values to the object. An integer will be initialized with 00, double with 0.00.0, boolean with falsefalse, and String with nullnull. It depends on the type of instance variable declared in the class from which an object is created.

Important

Let's consider another case where we'll create a parameterized constructor.

Compilation error:

We have created a parameterized constructor in our code, and we are trying to access the default constructor. It throws an error.

Note: When we create any constructor manually, then the compiler will not insert the default constructor. So, we'll also have to create the default constructor manually, if needed.

If we have created a parameterized constructor on our own and by any chance, we tried to call the default constructor, then it is our responsibility to also create our own default constructor as well.

But yes, this only occurs when we call the default constructor, whereas we have created parameterized constructor only. You can see the same in the code given below.

Output:

The problem didn't occur here because we neither created nor called the default constructor. We had a parameterized constructor and we are just calling it, so there isn't any need for default constructor.

Conclusion

  • A default constructor in Java is created automatically by the online Java compiler when the programmer doesn't create any constructor in the entire program.
  • It is created to assign the default values to the instance variables of the class when an object is created.
  • Default constructors are sometimes called no-arg constructors since they both work the same. But no-arg constructor is created by the user while default constructor can only be created by the compiler.