Constructor in Java

 

Constructor in Java programming language is a method which is used to initialize an object. When an object is created the java constructor is automatically called or invoked. Constructor is used to provide values or data to the objects. In other words we can say that constructor constructs the values for objects in object oriented programming.

 

Rules for creating Java constructors:

In Java programming language there are two rules that should be followed when defining constructors:

  1. The name of the constructor should be same as the name of its class.
  2. There should be no explicit return type of constructor.

 

Types of Java Constructors:

Constructor is a function whose name is same as the name of the class. Constructor has no return type but we can pass arguments and parameters. Constructors are used to remove the garbage values.

In Java programming language there are two types of constructors:

  1. Default constructor (having no arguments)
  2. Parameterized constructor (having arguments)

 

Default Constructor in Java:

When an object is created default constructor is automatically called. It has no parameters or arguments. The example of declaration of default constructor is as follows:

circle();

Default constructors are mostly used to initialize the values of data members to 0 so that the garbage values should be avoided. The following is the syntax of default constructor:

Syntax:

<name of the class> () { }

 

Example of default constructor in Java:

Consider the following example in which we have used a default constructor which will be called automatically when the object will be created:

CODE:

class Cycle {

Cycle () {

system. out. println (“Cycle is created”);

}

public static void main (string args []) {

Cycle c = new Cycle ();

}

}

OUTPUT:

Cycle is created

In the above example a class named Cycle is created that has one method and this method is called constructor. This constructor is a default constructor as it has no arguments. When the object is created in the main () method, the constructor is automatically called and hence the message that “Cycle is created” is printed.

In a class if the user does not define any constructor then the Java compiler will automatically create a default constructor.

Q. What is the purpose of default constructor?

The default constructor is used as it initializes the objects for example an integer type object will be initialized at 0 and a string with null etc.

 

Example of default constructor that displays the default values:

Consider the following example in which default values wiil be displayed by the default constructor:

class Employee {

int id;

string name;

public static void main (string [] args) {

Employee e = new Employee ();

system. out. println (e. id);

system. out. println (e. name);

}

}

OUTPUT:

0

null

In the above example, a class named ‘Employee’ is declared and it has two data members that are id and name. Then in the main method an object is created of the class ‘Employee’ with the keyword ‘new’ and named ‘e’. Using the object ‘e’, we printed values of id and name, the data members, of the class. In the above example it can be seen that the value that is printed of id is 0 this is because we did not initialized the variable id and also for the data member name we got null because we did not initialized it.

In the above example we did not create any constructor therefore, the compiler created a default constructor through which o and null is printed. 0 is printed for the integer type variable and null is printed for string type variable.

 

Parameterized Constructor in Java:

Parameterized constructor has parameters or arguments. The parameters differ the parameterized constructor from the default constructor. This constructor is called by objects when certain values are passed through the objects. Example:

circle(int a, int b);

Calling of constructor:

circle c(2, 3);

c is an object and has two parameters 2 and 3 that are passed to int a, and int b. Therefore, we do not need to input values from the user. For printing these values we simple use this object with the print function as:

c. print();

The values 2 and 3 are passed in the print function and will be printed.

Parameterized constructor is always used with default constructor.

 

Why use parameterized constructor?

In Java programming language the parameterized constructor is used to provide values to the objects. The parameterized constructor has arguments whose values are provided through the main function.

Example of Parameterized Constructor:

Consider the following example in which a class named Employee has declared and has a parameterized constructor:

CODE:

class Employee {

int no;

string name;

Employee (int n, string n1) {

no = n;

name = n1;

}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee e1 = new Employee (121, “Stuart”);

Employee e2 = new Employee (122, “Bill”);

e1. display ();

e2. display ();

}

}

OUTPUT:

121 Stuart

122 Bill

In the above example, a class named ‘Employee’ is declared that has two data members and one method or member function and a parameterized constructor. The record will be passed from the main function by using the objects as constructor arguments that is n and n1. Here n is the number of the employee and n1 is the name of the employee. These arguments will be assigned to the data members then in the parameterized constructor.

Then comes the method that is display () which is used to display the record of the employees. In the main function we created two objects to enter the records first and then to display the record. The objects get the memory in the heap area and the reference variables refer to the objects allocated in the heap memory. In the above example both e1 and e2 are the reference variables that refer to the objects allocated in memory.

Constructor overloading in Java:

In Java programming language we can overload constructors in a class. Overloading of constructors means that a number of constructors that have different number of parameters. The constructors are differentiated by the compiler according to the number of parameters in the constructor. The compiler also differentiates between the constructors by the data type of arguments they have.

Example of constructor overloading in Java:

Consider the following example in which we have created two constructors that has different number of arguments of different data type:

CODE:

class Employee {

int no;

string name;

int age;

Employee (int n, string n1) {

no = n;

name = n1;

}

Employee (int n, string n1, int a) {

no = n;

name = n1;

age = a;

}

void display () {

system. out. println (no + “ ” + name “ ” + age);

}

public static void main (sting args []) {

Employee e1 = new Employee (121, “Stuart”);

Employee e2 = new Employee (122, “Bill”, 52);

e1. display ();

e2. display ();

}

}

OUTPUT:

121 Stuart

122 Bill 52

In the above example, a class named ‘Employee’ is declared that has three data members and one method or member function and two parameterized constructors. The record will be passed from the main function by using the objects as constructor arguments that is n and n1 and a. Here n is the number of the employee and n1 is the name of the employee and ‘a’ is the age of the employee. These arguments will be assigned to the data members then in the parameterized constructors. The first constructor only has two parameters that are no, and name of the employee. The second constructor has three parameters that are no, name, and age. Which constructor to be called is decided by the compiler on the basis of number of parameters in the constructor.

When we pass two parameters that is the number and the name of the employee then the first constructor will be called and when three parameters are passed that is the number of the employee, the name of the employee and the age of the employee then the second constructor will be called. Objects will be used to call the constructors and parameters are passed in the object.

Then comes the method that is display () which is used to display the record of the employees. In the main function we created two objects to enter the records first and then to display the record. The objects get the memory in the heap area and the reference variables refer to the objects allocated in the heap memory. In the above example both e1 and e2 are the reference variables that refer to the objects allocated in memory.

Difference between constructor and method in Java:

In Java programming language or any other programming language there are some differences between constructors and the methods or member functions. The following are the differences between constructors and methods or member functions.

Constructors in Java Methods in Java
We use constructors in Java programming language to initialize the state of object. We use methods in Java programming language to expose the behavior of the objects in the program.
There is no return type of the constructor. Methods have return type.
The constructors are invoked implicitly. The methods are invoked explicitly.
If the program has no constructor then the compiler of java programming language will create a default constructor which will initialize the objects to 0 or null in case of strings. In Java programming language the methods are not provided by the compiler, the user has to define a method himself.
The name of the constructor should be the same as the name of its class. The name of the methods can be the name of the class or any other by user.
Constructors are invoked by the keyword ‘new’. Methods are not invoked by the keyword ‘new’.
Constructors are not called by name; a default constructor is called automatically when an object is created. Methods are called by name.
In Java programming language, the constructors (public or protected) are not inherited. In Java programming language, methods (public or protected) are inherited.

 

Similarities between constructors and methods in Java:

The following are the similarities between constructors and methods in Java programming language:

  1. Both constructors and methods have a list of parameters.
  2. Constructors and methods have a block of code that will be executed when these are called.
  3. Constructors and methods both can be overloaded and overridden in Java programming language.

 

Java Copy Constructor:

In Java programming language there is no copy constructor. In Java we can copy the values of one object in another like we did in C++ programming language using a copy constructor. In java programming language there are a number of ways to copy the values of objects to other objects. The following are some of the ways to copy values of object to other in Java:

  1. By using constructor
  2. By assigning the values of one object to the other object
  3. By using the clone () method of the object class

Consider the following example in which we have copied the values of one object to another object by using the java constructors:

CODE:

class Employee {

int no;

string name;

Employee (int n, string n1) {

no = n;

name = n1;

}

Employee (Employee e) {

no = e. id;

name = e. name;

}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee e1 = new Employee (121, “Stuart”);

Employee e2 = new Employee (e1);

e1. display ();

e2. display ();

}

}

OUTPUT:

121 Stuart

121 Stuart

In the above example, a class named ‘Employee’ is declared that has two data members and one method or member function and one parameterized constructor. The record will be passed from the main function by using the objects as constructor arguments that is n and n1. Here n is the number of the employee and n1 is the name of the employee. These arguments will be assigned to the data members then in the parameterized constructors.

In the code above, we copied the values of the object ‘e1’ to ‘e2’ through constructor. From the output it can be observed that when we used the object ‘e2’ to call the display method, the values that were passed by the object ‘e1’ were printed. Hence it is quite clear that the values of object ‘e1’ are copied to the object ‘e2’.

Then comes the method that is display () which is used to display the record of the employees. In the main function we created two objects to enter the records first and then to display the record. The objects get the memory in the heap area and the reference variables refer to the objects allocated in the heap memory. In the above example both e1 and e2 are the reference variables that refer to the objects allocated in memory.

Copy values without Constructor:

In Java programming language we can copy the values of one object to another object by simply assigning the values of object to the other object. When assigning values of one object to other we do not need to create constructor.

Consider the following example in which we have copied the contents of one object to other without using constructor:

CODE:

class Employee {

int no;

string name;

Employee (int n, string n1) {

no = n;

name = n1;

}

Employee () {}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee e1 = new Employee (121, “Stuart”);

Employee e2 = new Employee ();

e2. no = e1. no;

s2. name = e1. name;

e1. display ();

e2. display ();

}

}

OUTPUT:

121 Stuart

121 Stuart

In the above example, a class named ‘Employee’ is declared that has two data members and one method or member function and one parameterized constructor. The record will be passed from the main function by using the objects as constructor arguments that is n and n1. Here n is the number of the employee and n1 is the name of the employee. These arguments will be assigned to the data members then in the parameterized constructors.

In the code above, we copied the values of the object ‘e1’ to ‘e2’ without using constructor. From the output it can be observed that when we used the object ‘e2’ to call the display method, the values that were passed by the object ‘e1’ were printed. Hence it is quite clear that the values of object ‘e1’ are copied to the object ‘e2’. First we copied the no that was in the object e1 to e2 then the name of the employee from object e1 to object e2.

Then comes the method that is display () which is used to display the record of the employees. In the main function we created two objects to enter the records first and then to display the record. The objects get the memory in the heap area and the reference variables refer to the objects allocated in the heap memory. In the above example both e1 and e2 are the reference variables that refer to the objects allocated in memory.

 Q. Does Constructor return any value?

Ans. There is no return type of a constructor even then it returns a value, the constructor returns the instance of current class.

Can constructor perform other tasks instead of initialization?

In constructors any operation can be performed like the operations performed in a method for example objects can be created, calling a method and starting a thread, etc.