A constructor is a special method which plays an important role when objects of a class are created. Java Constructors are basically used to allocate memory for the objects and initialize these objects to the desired values at the time of creation. It is not compulsory to have a constructor in a Java class. If a constructor is not defined then all member variables will be initialised to their default values i.e. numeric data type variables are set to 0, char data type variables are set to null character (‘\0’) and reference variables are set to null.
When is a Constructor Called?
Each time a new object of a class is created using the new keyword at least one constructor (say default constructor) is called automatically and the values defined in that constructor are assigned to various data members of that class.
Features of a Java Constructor
The constructor has the following main features:
- It is a special method that has no return type (not even void).
- The name of a constructor is always the same as that of the class in which it resides.
- It is called automatically when an object of that class is created.
- We can have more than one constructor in our class, as long as the number of arguments or the type of arguments is different. This feature is referred to as constructor overloading.
Types of Constructors
There are two types of constructors in java. These are:
The general syntax for declaring constructors is as follows:
[Access Specifier] Class_Name ([Parameters List])
{
// Body of constructor
}
Default Constructor (Non Parameterized Constructors)
As the name suggests, these constructors have no parameters and hence they do not accept any arguments. If we do not define any constructor in a class then the compiler automatically creates a default constructor for the class. Default constructor will initialize default values to the member variables depending on their data type.
Syntax of a default constructor is:
class_name () {
// body of constructor
}
Example of Default Constructor
class constEg { int rollno; String name; // Default Constructor constEg() { System.out.println("Constructor Called"); } void show() { System.out.println("Roll num = " + rollno); System.out.println("Name = " + name); } public static void main(String args []) { constEg obj = new constEg(); obj.show(); } }
Output
Parameterized Constructors
A constructor that has parameters is called a parameterized constructor. In real world programming, the program must work in a flexible manner allowing the user to construct distinct objects having various dimensions. This is possible when parameters are passed to the constructor. Therefore, we use parameterized constructors.
Syntax of a parameterized constructor is:
class_name (list of parameters) {
// body of constructor
}
Example of Parameterized Constructor
class stu { int rollno; String name; // Parameterized Constructor stu(int r, String n) { System.out.println("Constructor Called"); rollno = r; name = n; } void show() { System.out.println("Roll num = " + rollno); System.out.println("Name = " + name); } public static void main(String args []) { stu s1 = new stu(1, "abc"); s1.show(); stu s2 = new stu(2, "xyz"); s2.show(); } }
Output:
Constructor Overloading
Just like methods, we can also overload constructors for creating objects in different ways. Constructor overloading is a technique in java in which a class can have more than one constructor defined in it that have different parameter list. The compiler differentiates between various constructors based on the number of parameters, type of parameters and the order of parameters.
For example: Some of the valid constructors that can be defined are:
Account (int a);
Account (int a, String name);
Account (int a, float bal);
class stuEg { int rollno; String name; int age; // Parameterized Constructor 1 stuEg(int r, String n) { System.out.println("Constructor Called"); rollno = r; name = n; } // Parameterized Constructor 2 stuEg(int r, String n, int a) { System.out.println("Constructor Called"); rollno = r; name = n; age = a; } void show() { System.out.println("Roll num = " + rollno); System.out.println("Name = " + name); System.out.println("Age = " + age); } public static void main(String args []) { stuEg s1 = new stuEg(1, "abc"); s1.show(); stuEg s2 = new stuEg(2, "xyz", 10); s2.show(); } }