In this tutorial, we will discuss What are Constructors in Java. We will understand how they are useful and know their implementation in a Java program.
What are Constructors in Java?
A Constructor in a Java program is used to initialize an object. Any object that we create in Java first calls the constructor of that class to initialize the object.
A Constructor has a block that can contain programming statements that are generally used to initialize that object.
Let’s understand this with an example.
class TagLine{ String tag; TagLine(String tg) { tag = tg; } public void print() { System.out.println("TagLine is: " + tag); } public static void main(String agmts[]) { TagLine n= new TagLine("UseMyNotes"); n.print(); } }
In the above example, we can see that there is a constructor which takes one input parameter ‘tg’ in the form of a String. This value is assigned to the ‘tag’ variable of the class. We also see a print() method that is being called in the main() method which then prints the tagline using the ‘tag’ variable of the object.
Working of Constructors in the context of the example above:
- At first, the main() method is called in a program.
- The Object of the TagLine class is created the constructor is passed with one argument “UseMyNotes”.
- During the creation of the object, the first method that is called is the constructor of that TagLine class. The constructor takes that one parameter (UseMyNotes) and then assigns it to the appropriate variable.
- The main() method then calls the print() method of the object which then prints the tagline.
What to know about constructors?
- A constructor’s name must be the same as the class name in which it resides.
- The constructor should not have any return type.
- A constructor can have any number of input parameters.
- It can be overloaded just like any other method.
Is there more than one constructor in a class?
Yes, there can be more than one constructor or no constructor in a class.
In the case where there is more than one constructor, each one of them should have,
- A different number of parameters are taken as input.
- Different order of parameter type in case of the same number of input parameters.
What are the types of constructors in Java?
There are 2 types of constructors in Java, they are:
- No-argument Constructor
- Parameterized Constructor
No-argument Constructor
A No-Argument Constructor is a constructor that takes no input parameters.
Example:
class Company { String companyName; Company() { companyName= "Use My Notes"; } public void printCompanyName() { System.out.println(companyName); } public static void main(String agmts[]) { //Constructor takes no arguments Company c = new Company(); c.printCompanyName(); } } /* Use My Notes */
The example above shows that the creation of the Company object takes no parameters for the constructor. The constructor has a fixed name that is not being changed by any parameter of the constructor. So, the object is being initialized statically. With this, the ‘companyName’ variable of the object gets initialized with “Use My Notes” and later gets printed with the printCompanyName() method.
Parameterized Constructor
A Parameterized Constructor takes one or more input parameters which is the creation of the object of a class. These input parameters are generally used to dynamically construct the object and set it to a specific state.
Example:
class Company { String companyName; Company(String n) { if(n == null) { companyName= "Use My Notes"; } else { companyName= n; } } public void printCompanyName() { System.out.println(companyName); } public static void main(String agmts[]) { //Constructor takes no arguments Company c = new Company("MyCompany"); c.printCompanyName(); } } /* MyCompany */
The above example clearly shows that the constructor of the Company class takes a string as an argument. The creation of the Company object is passed with the string “MyCompany”. This string is used in the constructor for assigning the ‘companyName variable of the object with the given string.
What happens when there are no constructors in a class?
In case there are no constructors in a class, then the Java compiler automatically assigns a default constructor to that class during the compile time. This default constructor accepts no arguments and has an empty body.
There is another scenario when there is one or more parameterized constructors in a class and there isn’t any non-parameterized constructor. In this case, if we call the default constructor (the one that accepts no arguments) then the program will raise an error during the compile time of the program.