13 October 2020

What is a constructor in Java?

Welcome to another tutorial on the "How to code in Java" tutorial series. In the previous tutorial, we have gone through classes and objects. In this tutorial, we'll be continuing on what is a constructor and how we can utilise it in our code. This tutorial is the continuing of the previous tutorial, it'll be much easier to follow through this tutorial if you've read it. So I if you can't follow along in this tutorial, please have a read on the previous tutorial


Index for the tutorial:



What is a constructor?


A constructor is a unique method which is called during object creation. It'll be automatically called whenever you create a new object for the class upon using the "new" keyword. To define a constructor method all, you need to do is to make a method:
  • Must have the same name as the class.
  • Cannot have a return type.
  • But we can specify an access modifier for it.
  • It can't be abstract, static, final, or even synchronised.


Type of constructor.


No-arguments constructor.


When you create a new object/instance from a class, the creation of the object will first call the constructor provided by the class. If you didn't specify any constructor for that class,  then the compiler would automatically create a no-arguments constructor (default constructor) for you. If you do create a constructor for the class with and without specifying any parameter for it, the compiler will use this constructor instead, and the compiler will not generate the default constructor. 

Using back the example from the previous tutorial, where we created a "Dog" class as well as an instance of it called "dog". Let's create a no-arguments constructor for the class, and make the constructor print out "Created a dog object" text to the terminal when a new object is created.

Adding a constructor to the "Dog" class

Creating a new object for the class

The constructor is called upon object creation.

As you can see, we just create a new object and nothing more, and the constructor method is called automatically. 

The default constructor also provides the default values to the object's attributes like 0, null, etc. depending on the attribute's data type.

Testing for object default value;

Result for testing the default values.


Parameterised constructor.


A constructor that has parameter included with it is called a parameterised constructor. Since the no-arguments constructor is used to initialise the object's attributes with default values (null, 0, etc.). A parameterised constructor is usually used to provide the object's attribute with different values.

Changing the constructor to accept some parameters.

Testing the parameterised constructor.

Result after compiling the code.

One thing to caution about is to correctly place the parameter and arguments in the correct order and number. If you somehow miss one of the parameters or added in an incorrect data type value, an error will be produced. The error will be shown during code editing as well as during code compilation.

This piece of code has an error because we miss out a parameter for the constructor.

The error that compiler returned when we try to compile the code.


There's an error here because we used the wrong type for the final argument.
It's supposed to be a char type, but we gave it a string type instead.

Type error returned by the compiler on code compilation.


Difference between method and constructor.



The last two points will be discussed in a future tutorial, where we discuss inheritances and method overloading.


Full code.




Well, that's about all the basic things you need to know about constructor. I hope you now know what can a constructor do and when to use them. If you find this tutorial helpful, please share it with others that might need it or are interested in it. Also, if you have any critics, suggestion or question, feel free to leave a comment down below. In the next tutorial, we finally will dive deeper into access modifier before we get started with our first OOP concept, abstraction. So stay tuned.

No comments:

Post a Comment