Constructor in Java: Complete Notes, Types, Rules and Examples
Learn Constructor in Java with simple notes, syntax, types, rules and practical examples. Understand default, parameterized and copy constructors in Core Java.
A constructor in Java is a special block that is mainly used to initialize an object. When an object is created using the new keyword, its constructor is called automatically.
The main purpose of a constructor is object initialization, which means assigning meaningful values to the instance variables of an object. A constructor itself does not create the object; object creation happens through the new keyword and constructor initialization follows as part of object creation.
What is a Constructor in Java?
A constructor is a special member of a class whose name is the same as the class name.
For example:
class Student {
Student() {
System.out.println("Constructor Called");
}
public static void main(String[] args) {
Student s = new Student();
}
}
Output
Constructor Called
When the following line runs:
Student s = new Student();
an object of the Student class is created and the constructor is called automatically.
Properties of Constructor
A constructor has the following important properties:
1. The constructor name must be the same as the class name.
2. A constructor is called automatically when an object is created.
3. A constructor does not have a return type.
4. If a return type is written, it becomes a normal method instead of a constructor.
5. A constructor can have parameters.
6. Constructors can be overloaded.
7. Constructors are not inherited.
8. Constructors cannot be overridden.
Constructor Does Not Have a Return Type
Consider the following example:
class Test {
void Test() {
System.out.println("Hello");
}
public static void main(String[] args) {
new Test();
}
}
Here:
void Test()
is not a constructor.
Because void is used, Java treats Test() as a normal method.
The method will not execute automatically when the object is created.
To call it, we need to call it explicitly:
Test t = new Test();
t.Test();
Important Point
Test() → Constructor
void Test() → Method
int Test() → Method
String Test() → Method
Types of Constructors in Java
Commonly, constructors are understood in two forms:
Default Constructor
Parameterized Constructor
Let's understand both one by one.
Default Constructor in Java
If a class does not contain any constructor, the Java compiler automatically provides a constructor. This is called the default constructor.
For example:
class Test {
}
Conceptually, the compiler provides a no-argument constructor similar to:
class Test {
Test() {
super();
}
}
Now we can create an object without any problem:
class Test {
public static void main(String[] args) {
Test t = new Test();
}
}
Important Rule About Default Constructor
The compiler provides a default constructor only when no constructor is written by the programmer.
Example:
class Student {
Student() {
System.out.println("Student Constructor");
}
}
In this case, the compiler will not add another default constructor because a constructor is already present.
Similarly:
class Student {
Student(int id) {
System.out.println(id);
}
}
The compiler will not automatically add:
Student() {
}
This is an important point while working with constructor overloading and inheritance.
Parameterized Constructor in Java
A constructor that accepts one or more arguments is called a parameterized constructor.
Example:
class Student {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
public static void main(String[] args) {
Student s = new Student(101, "Manoj");
System.out.println(s.id);
System.out.println(s.name);
}
}
101
Manoj
Here:
Student(int id, String name)
accepts values when the object is created and uses those values to initialize the object's instance variables.
Why Do We Use this Keyword in Constructor?
Suppose instance variables and constructor parameters have the same names.
class Student {
int rollNo;
int age;
Student(int rollNo, int age) {
rollNo = rollNo;
age = age;
}
}
This code does not initialize the instance variables properly.
The parameters refer to themselves because the local parameter names take precedence in that scope.
To access the current object's instance variables, we use this.
class Student {
int rollNo;
int age;
Student(int rollNo, int age) {
this.rollNo = rollNo;
this.age = age;
}
public static void main(String[] args) {
Student s = new Student(1, 20);
System.out.println(s.rollNo);
System.out.println(s.age);
}
}
Output
1
20