CONSTRUCTOR IN JAVA
1. Introduction to Constructor
A constructor is a special member of a class in Java. It is automatically invoked when an object of
the class is created. The main purpose of a constructor is to initialize the data members or
instance variables of an object.
The constructor has the same name as the class. It does not have any return type, not even void.
Simple definition: A constructor is a special member of a class used to give initial values to an
object at the time of object creation.
General Syntax
class ClassName
{
ClassName()
{
// constructor statements
}
}
In the above syntax, the class name and constructor name are the same.
Example 1: Simple Constructor
class Student
{
int rollNo;
Student()
{
rollNo = 101;
}
void display()
{
[Link]("Roll Number = " + rollNo);
}
public static void main(String args[])
{
Student s = new Student();
[Link]();
}
}
Output:
Roll Number = 101
Explanation: Student() is the constructor. When the statement Student s = new Student(); is
executed, an object named s is created. At the same time, the constructor is automatically invoked.
The constructor assigns the value 101 to rollNo.
2. Need for Using a Constructor
When an object is created, its instance variables need suitable initial values. A constructor provides
these initial values automatically. Therefore, constructors are mainly used for object initialization.
Without Using a User-Defined Constructor
class Student
{
int rollNo;
String name;
public static void main(String args[])
{
Student s = new Student();
[Link] = 101;
[Link] = "John";
[Link]([Link]);
[Link]([Link]);
}
}
In this program, the values are assigned separately after creating the object. If many objects are
created, the same type of initialization statements may have to be written repeatedly.
Using a Constructor
class Student
{
int rollNo;
String name;
Student()
{
rollNo = 101;
name = "John";
}
void display()
{
[Link]("Roll Number = " + rollNo);
[Link]("Name = " + name);
}
public static void main(String args[])
{
Student s = new Student();
[Link]();
}
}
Output:
Roll Number = 101
Name = John
Explanation: The values of rollNo and name are initialized inside the constructor. When the object is
created, Java automatically executes the constructor and gives the initial values to the object.
Why Do We Need Constructors?
1. To initialize an object: A constructor gives initial values to the instance variables of an object.
2. Automatic execution: The programmer does not need to call the constructor separately. It is
invoked automatically during object creation.
3. To reduce repeated statements: Initialization statements can be placed inside the constructor
instead of writing them repeatedly.
4. To ensure proper initial values: A constructor helps an object start with meaningful values.
5. To make the program organized: Object initialization is kept in one specific place, making the
program easier to understand and maintain.
Example 2: Real-Life Idea
Consider a bank account. When a bank account object is created, the account holder's name and
balance may need initial values. A constructor can initialize these values immediately.
class Account
{
String name;
double balance;
Account()
{
name = "Ravi";
balance = 5000.0;
}
void display()
{
[Link]("Name = " + name);
[Link]("Balance = " + balance);
}
public static void main(String args[])
{
Account a = new Account();
[Link]();
}
}
Here, the Account object receives its initial name and balance through the constructor.
3. Invoking a Constructor
Invoking means calling or executing. A constructor is invoked automatically when an object is
created using the new keyword.
Syntax
ClassName objectName = new ClassName();
Example
Student s = new Student();
Student is the class name.
s is the object reference.
new creates a new object.
Student() invokes the constructor.
Example 3: Constructor Invocation
class Demo
{
Demo()
{
[Link]("Constructor is invoked");
}
public static void main(String args[])
{
Demo d = new Demo();
}
}
Output:
Constructor is invoked
Step-by-step explanation:
1. Program execution starts from the main() method.
2. Java executes Demo d = new Demo();.
3. The new keyword creates a Demo object in memory.
4. Demo() is automatically invoked.
5. The statement inside the constructor is executed.
Important: A constructor is not normally called like an ordinary method. It is invoked as part of the
object creation process.
Invoking a Parameterized Constructor
class Student
{
int rollNo;
Student(int r)
{
rollNo = r;
}
public static void main(String args[])
{
Student s = new Student(105);
[Link]("Roll Number = " + [Link]);
}
}
The value 105 is passed to the constructor. Therefore, Student(int r) is invoked and the value is
assigned to rollNo.
4. Features / Characteristics of a Constructor
1. A Constructor Has the Same Name as the Class
The constructor name must be exactly the same as the class name.
class Student
{
Student()
{
[Link]("Constructor");
}
}
The class name is Student and the constructor name is also Student.
2. A Constructor Has No Return Type
A constructor does not have a return type. Even void must not be written before the constructor
name.
Student() // Correct
{
}
void Student() // Not a constructor; treated as a method
{
}
3. A Constructor Is Automatically Invoked
A constructor is automatically executed when an object is created.
Student s = new Student();
The constructor Student() is invoked automatically.
4. A Constructor Is Used to Initialize Objects
The main purpose of a constructor is to initialize instance variables.
class Book
{
int price;
Book()
{
price = 500;
}
}
The constructor initializes the price variable with 500.
5. A Constructor Can Have Parameters
A constructor can receive values through parameters. Such a constructor is called a parameterized
constructor.
class Student
{
int rollNo;
Student(int r)
{
rollNo = r;
}
}
6. Constructors Can Be Overloaded
A class can contain more than one constructor. The constructors must have different parameter lists.
This is called constructor overloading.
class Student
{
Student()
{
[Link]("No argument constructor");
}
Student(int x)
{
[Link]("Parameterized constructor");
}
}
Java selects the suitable constructor based on the arguments passed during object creation.
7. A Constructor Is Not Inherited
Constructors belong to their own class and are not inherited by a child class. However, a child class
constructor can call the parent class constructor using super().
8. A Constructor Can Use Access Modifiers
A constructor may be declared using access modifiers such as public, private, or protected. It can
also have default access.
public Student()
{
}
9. The this() Statement Can Call Another Constructor
The this() statement is used to call another constructor of the same class. It must be the first
statement in the constructor.
class Demo
{
Demo()
{
this(10);
[Link]("First constructor");
}
Demo(int x)
{
[Link]("Value = " + x);
}
}
10. The super() Statement Can Call the Parent Constructor
The super() statement is used to call a constructor of the parent class. It must be the first statement
in the child class constructor when written explicitly.
11. A Constructor Executes During Object Creation
Each time an object is created, the appropriate constructor is executed.
Student s1 = new Student();
Student s2 = new Student();
If the no-argument constructor is selected in both statements, it executes once for s1 and once for s2.
Quick Revision
Constructor: A special member of a class used to initialize objects.
Need: To provide proper initial values automatically.
Invocation: Automatically invoked when an object is created using new.
Main features: Same name as class, no return type, automatic invocation, object initialization,
parameter support, and constructor overloading.
Conclusion
A constructor is an important concept in Java object-oriented programming. It initializes an object at
the time of object creation. Since it is automatically invoked, it provides a simple and organized way to
assign initial values to instance variables. Understanding the need, invocation, and features of
constructors is essential before learning constructor overloading and other advanced object-oriented
concepts.