Constructors
Constructors
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.
Unlike Java methods, a constructor has the same name as that of the class and does not have any return
type.
Note: It is not necessary to write a constructor for a class. It is because the java compiler creates a default
constructor (constructor with no arguments) if your class doesn’t have any.
class Main {
private String name;
// constructor
Main() {
System.out.println("Constructor Called:");
name = "Hello Students";
}
Output:
Constructor Called:
Hello Students
Public Constructor:
Constructors must have the same name as the class within which it is defined it is not necessary for
the method in Java.
Constructors do not return any type while method(s) have the return type or void if does not return
any value.
Constructors are called only once at the time of Object creation while method(s) can be called any
number of times.
Now let us come up with the syntax for the constructor being invoked at the time of object or
instance creation.
class Geek
{
.......
// A Constructor
Geek() {
}
.......
}
// We can create an object of the above class using the below statement. This
statement calls above constructor.
Each time an object is created using a new() keyword, at least one constructor (it could be the default
constructor) is invoked to assign initial values to the data members of the same class. Rules for
writing constructors are as follows:
The constructor(s) of a class must have the same name as the class name in which it resides.
A constructor in Java can not be abstract, final, static, or Synchronized.
Access modifiers can be used in constructor declaration to control its access i.e which other class can
call the constructor.
So by far, we have learned constructors are used to initialize the object‟s state. Like methods, a
constructor also contains a collection of statements(i.e. instructions) that are executed at the time of
Object creation.
There are no “return value” statements in the constructor, but the constructor returns the current class
instance. We can write „return‟ inside a constructor.
Default Constructor
Parameterized Constructor
Copy Constructor
A constructor that has no parameters is known as default the constructor. A default constructor is
invisible. And if we write a constructor with no arguments, the compiler does not create a default
constructor. It is taken out. It is being overloaded and called a parameterized constructor. The default
constructor changed into the parameterized constructor. But Parameterized constructor can‟t change
the default constructor.
Example:
// Driver class
class GFG {
// Default Constructor
GFG() { System.out.println("Default constructor"); }
// Driver function
public static void main(String[] args)
{
GFG hello = new GFG();
}
}
Output:
Default constructor
Example:
// Java Program for Parameterized Constructor
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Geek geek1 = new Geek("avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
}
}
Output
GeekName :avinash and GeekId :68
Example2:
// Java Program to illustrate constructor overloading
// using same task (addition operation ) for different
// types of arguments.
import java.io.*;
class Geek {
// constructor with one argument
Geek(String name)
{
System.out.println("Constructor with one "
+ "argument - String : " + name);
}
System.out.println(
"Constructor with two arguments : "
+ " String and Integer : " + name + " " + age);
}
class GFG {
public static void main(String[] args)
{
// Creating the objects of the class named 'Geek'
// by passing different arguments
// Invoke the constructor with one argument of
// type 'String'.
Geek geek2 = new Geek("Shikhar");
Output:
Output
Constructor with one argument - String : Shikhar
Constructor with two arguments : String and Integer : Dharmesh 26
Constructor with one argument : Long : 325614567
Unlike other constructors copy constructor is passed with another object which copies the data
available from the passed object to the newly created object.
A copy constructor is a special type constructor which creates an object using another object of the same
class. In return, it gives a copy of an already created object that is copied.
Example 1:
class Rectangle
{
private double length;
private double breadth;
//copy constructor
Rectangle(Rectangle rect)
{
System.out.println("Copy Constructor Invoked!!");
length = rect.length;
breadth = rect.breadth;
}
//method to return area
double calculateArea()
{
return length*breadth;
}
}
class Rectangle_Main{
Example 2:
// Java Program for Copy Constructor
import java.io.*;
class Student {
// data members of the class.
String name;
int id;
// Parameterized Constructor
Student (String n, int a)
{
name = n;
id = a;
}
// Copy Constructor
Student (Student obj2)
{
name = obj2.name;
id = obj2.id;
}
}
class College {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
System.out.println("First Object");
Student S1 = new Student ("avinash", 68);
System.out.println("Name :" + S1.name + " and Id :" + S1.id);
System.out.println();
Output:
First Object
GeekName :avinash and GeekId :68
Constructor overloading in Java is a technique of having more than one constructor with different
parameter lists. They are arranged in a way that each constructor performs a different task. They are
differentiated by the compiler by the number of parameters in the list and their types.
Similar to Java method overloading, we can also create two or more constructors with different
parameters. This is called constructors overloading.
Output:
111 Karan 0
222 Aryan 25