Constructors in Java

What is a Constructor? 

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. 

Constructor is a particular method whose name is the same as the class name. 

Syntax:

class World 
{ 
World( ) 
{ 
……//lines of code//….. 
} 
} 

Rules for Creating Java Constructor:

  1. The primary purpose of the constructor is to initialize the object.
  2. Every java class has a constructor. 
  3. A constructor is called at the time of object creation. 
  4. A constructor does not contain any return type except the void. 

For Example:-

Java Code

package World.java; 
class Student1{ 
int roll;String name; 
Student1() { 
roll=1;name=null; 
} 
void display() 
{ 
System.out.print(roll+" "+name); 
} 
} 
class Student2 
{ 
public static void main(String[] args) {
Student1 ref=new Student1(); 
ref.display(); 
} 
} 

output:1 null

In the above example, we have a constructor same as classname(Student1).At the time of object creation (i.e.,Student1 ref=new Student1()) ,the constructor(Student1) will call itself. But display() is a method, so the user needs to call it using the reference(ref) variable and using the (. )operator.

Types of Constructors:

There are two types of constructors in Java: 

  1. Default constructor (no-arg constructor) 
  2. Parameterized constructor 
  • Default Constructor: A constructor that has no parameter is called a default parameter. 

Code:

Java Code

class Student1{ 
int num;//instance variables 
String name; 
// this would be invoked while an object 
// of that class is created. 
Student1() 
{ 
} 
void Display()
{ 
System.out.println(num+" "+name); 
} 
} 
class Student2 { 
public static void main(String[] args) 
{ 
// this would invoke the default constructor. 
Student1 ref = new Student1(); 
//calling a method, 
ref.Display(); 
} 
} 

Output:0 null

In the above example, we are not creating any constructor so the compiler provides you with a default constructor. Here 0 and null values are provided by the default constructor. 

  • Parameterized Constructor: A constructor which has a specific number of parameters is called a parameterized constructor. The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.

Code:

Java Code

class Student1{ 
String name; 
int id; 
Student1(String name, int id)//creating a parameterized constructor 
{ 
this.name = name; 
this.id = id; 
} 
//method to display the values 
void Display() 
{ 
System.out.println(name+" "+id); 
} 
} 
// Class 2 
class Student2 { 
// main driver method 
public static void main(String[] args) 
{ 
//creating objects and passing values 
Student1 ref = new Student1("adam", 1); 
//calling method to display the value of the object 
ref.Display(); 
} 
} 

Output: adam 1

In the above example, we have created the constructor of the Student class that has two parameters. We can have any number of parameters in the constructor.

Constructor Overloading: 

A constructor overloading is a technique of having more than one constructor with either different parameters or different no. of parameters. 

Code:

Java Code

class Student1{ 
String name;//instance variables 
int id; 
double marks; 
Student1()//creating a constructor 
{ 
name="Ankush";id=45;marks=77.7; 
} 
Student1(int x)//creating one agr coonstructor 
{ 
id=x; 
} 
Student1(double y, String z)//creating two args constructor 
{ 
marks=y;name=z; 
} 
void Display() 
{ 
System.out.println(name+" "+id); 
} 
} 
// Class 2 
class Student2 { 
// main driver method 
public static void main(String[] args) 
{ 
Student1 ref=new Student1(); 
Student1 ref2=new Student1(10); 
Student1 ref3=new Student1(23.45,"Ankush"); 
System.out.println(ref.id+" "+ref2.marks+" "+ref3.name); 
} 
} 

output: 

45 0.0 Ankush

In the above example, we have created more than one constructor with different parameters(or no. of parameters)

Copy Constructor: 

Whenever we pass an object reference to a constructor, then it is called a copy constructor.

Syntax:

<class_name>(object ref){};

Code:

Java Code

class Student1{ 
String name; 
int id; 
Student1() 
{ 
name="Ankush";id=45; 
System.out.println(name+" "+id); 
} 
Student1(Student1 ref) 
{ 
name=ref.name; 
id=ref.id; 
System.out.println(name+" "+id); 
} 
} 
// Class 2 
class Student2 
{ 
// main driver method 
public static void main(String[] args) 
{ 
Student1 r=new Student1(); 
Student1 r2=new Student1(r); 
} 
} 

output

Ankush 45 

Ankush 45

Special thanks to Divyanshi Pal for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article