DEV Community

Cover image for Constructors

Posted on


Some background

I am currently attending Flatiron school and here at this program they introduced their students to constructors. Although I was able to learn what a constructor is I didn't really get the shape of what a constructor is used for. This implored me to ask a friend of mine who works at Amazon currently how important constructors are. He told me that he took a similar journey as mine. He started from the ground and self-taught and moved from job to job until he landed one at Amazon. He told me the importance of constructors that I feel could benefit everyone.

What is a constructor?

A constructor is a special type of procedure to create an object. It prepares object made by a constructor to be used later in your code. Additionally, constructors take in arguments that are used to set variable requirements.

Are there any rules to a constructor?

Indeed, there are 4 basic rules or principles that should be followed in order to ensure that your code is as clean as possible.


  1. The name of the constructor should be the same as that of the class name.
  2. A constructor cannot be declared as final, static, synchronized, or abstract type.
  3. It cannot have an explicit return type.
  4. A constructor can have an access modifier to control the access.

Are there different types of constructors?

Certainly, there are 3 different types of Constructors. Default, No-Args, and Parameterized.

This is a default constructor

Image description
Nothing special here a default constructor is what JavaScript builds itself if the constructor is not assigned a class.

This is a No-Args constructor

Image description
The No-Args constructor is quite similar to the default constructor, however the main difference is the that the body of this constructor can have any code where a default constructor can not. In fact, in this example the body of this constructor is empty and there is no value assigned.

This is a Parameterized constructor

Image description
Parameterized constructors are constructors that exist with passed arguments or parameters.

Key Differences between Constructors and methods.

Some of you may have been able to point out the similarities between methods and constructors. However, there are some differences that are important.

  1. The constructor class must be the same name as the Class name.
  2. Constructors do not have a return type.
  3. Constructor is only called when the object is created.


Constructors can be used within classes to contain unique data to when that new object is created. Since constructors are used to set unique data and assigning them to properties allows them to be passed to other class instances.

Here is the link to an article that I found that might better explain constructors by Yasir Khatri.

Top comments (0)