The Java Constructor is a block of code that allows you to initialize the instance variables of it`s class. A constructor is like a special method with same name as class name and does not contain an explicit return type. The use of constructor is to assign values to instance variables.
Syntax:
< access modifier > constructorname()
{
//constructor body
}
Example:
class Sample
{
Sample()
{
// constructor body
}
}
A constructor name must be same as class name.
A constructor should not have an explicit return type and not even void. If we provide a return type to a constructor then java will consider it as a method and not the constructor.
If you as a developer did not write / provide any constructor in a class then Java compiler internally will add a default constructor implicitly. But if you wrote a constructor of any type in a class then compiler will not add any constructor implicitly into the class. Therefore if you are writing only the parameterized constructors and you want to use a no-argument constructor while creating an object then you must write a no –argument constructor too.
The constructors get called at the time of object creation and in constructor chaining.
The constructors can be overloaded but you cannot override constructors.
There are three types of constructors.
Default / No –argument constructor.
Parameterized constructor.
Copy constructor.
Note: The Java does not support / provides copy constructor, but as a developer we can implement the copy constructor in Java.
Default / no –argument constructor.
If you did not add any constructor to a class then Java compiler implicitly add a no- argument constructor called default constructor. If you wrote a constructor without any parameter in a class then that constructor is called as a no argument constructor.
Example:
class Sample
{
// 0- argument constructor
Sample()
{
// constructor body
}
}
Parameterized Constructor
If you define a constructor with some parameters then that constructor is called as a parameterized constructor. The parameterized constructors are used to assign the user values to the instance variables at the time of an object creation.
Example:
class Student
{
// instance variables
int rollNo;
String name;
// Parameterized constructor
Student(int rollNo, String name)
{
// assigning user values to instance variables
this.rollNo= rollNo;
this.name= name;
}
// main() method
public static void main(String[] args)
{
//creating an object of Student class
Student stud= new Student(10, "Rajveer");
//Printing values of instance variables
System.out.println("Roll Number="+stud.rollNo+ ", Name= "+stud.name);
}
}
Copy Constructor
The copy constructor creates a new object by taking the values of an existing object in other words a copy constructor creates a duplicate copy of an object.
Note: The java language does not provide the copy constructor instead they provided the concept of object cloning to create an exact duplicate object from an existing object.
Even though the Java did not provide the copy constructors to create duplicate objects from an existing object but we can implement a copy constructor in Java.
How to implement copy constructor in Java language?
To implement the concept of copy constructors in java; we need to define a constructor which will accept the reference variable as a parameter.
Understand the below copy constructor example, here we have two constructors first constructor is accepting two parameters rollNo and name, and second constructor is taking a current class type reference variable as a parameter.
Inside the main() method, first statement is creating an object by providing values 10 & “Rajveer”, and the second statement is creating an object by passing the reference of a existing object, this will call the second / copy constructor. In the copy constructor we are getting the roll number and name values of first/ existing object and assigning those values to the newly created object. Therefore, both objects will have the same values, as we printed the values of both objects that will print the same values. So we can say a copy constructor is creating an object by using the values of an existing object.
class Student
{
// instance variables
int rollNo;
String name;
// Parameterized constructor
Student(int rollNo, String name)
{
// assigning user values to instance variables
this.rollNo= rollNo;
this.name= name;
}
// Copy constructor
Student(Student stud)
{
// assigning values of existing object to instance variables
rollNo=stud.rollNo;
name= stud.name;
}
// main() method
public static void main(String[] args)
{
//creating an object of Student class
Student stud1= new Student(10, "Rajveer");
//creating a new object from an existing one
Student stud2= new Student(stud1);
//Printing values of instance variables
System.out.println("Roll Number="+stud1.rollNo+ ", Name= "+stud1.name);
//Printing values of new object
System.out.println("Roll Number="+stud2.rollNo+ ", Name= "+stud2.name);
}
}
How to copy values of an object into another object without using cloning and copy constructors in Java language?
The following example shows how we can easily copy the values of an object into another object.
class Student
{
// instance variables
int rollNo;
String name;
// 0 parameterized constructor
Student()
{
}
// Parameterized constructor
Student(int rollNo, String name)
{
// assigning user values to instance variables
this.rollNo= rollNo;
this.name= name;
}
// main() method
public static void main(String[] args)
{
//creating an object of Student class
Student stud1= new Student(10, "Rajveer");
//creating a new object
Student stud2= new Student();
// assigning values of existing object to instance variables
stud2.rollNo=stud1.rollNo;
stud2.name= stud1.name;
//Printing values of instance variables of first object
System.out.println("Roll Number="+stud1.rollNo+ ", Name= "+stud1.name);
//Printing values of instance variables of new object
System.out.println("Roll Number="+stud2.rollNo+ ", Name= "+stud2.name);
}
}