The Java is one of the most popular object-oriented programming languages in the world. One of the most important concepts in Java is the constructor. The Constructors are used to initialize objects & play a major role in object-oriented programming.
What is a Constructor in Java?
A constructor in Java is a special type of
method that is automatically called when an object of a class is created. Its
main purpose is to initialize object variables & prepare the object for
use.
A constructor:
- Does not have a return type
- Executes automatically during object creation
The Constructors are extremely useful because they allow developers to initialize objects with required values immediately.
Syntax of Constructor
class ClassName {
ClassName() {
// constructor body
}
}
Example :
class Student {
String name;
Student() {
name = "kumar";
}
void display() {
System.out.println(name);
}
public static void main(String[]
args) {
Student s1 = new Student();
s1.display();
}
}
Output
kumar
In this example:
- Student() is the constructor
- It initializes the variable name
· The constructor is automatically called when the object is created
Why Constructors are Important
The Constructors are important because:
- They initialize objects automatically
- They reduce repetitive code
- They improve readability
Types of Constructors in Java
The Java mainly supports the following types of
constructors:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
1. Default Constructor
If you do not create any constructor in a
class Java automatically creates one internally. This is called the default
constructor.
Example
class Demo {
void display() {
System.out.println("Hello
Java");
}
public static void main(String[]
args) {
Demo d = new Demo();
d.display();
}
}
In this example:
- No constructor is written by the programmer
- The Java automatically provides a default constructor
2. Parameterized Constructor
A constructor that accepts arguments is
known as a parameterized constructor. The Parameterized constructors are used
to initialize objects with different values.
Example
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + "
" + age);
}
public static void main(String[]
args) {
Student s1 = new Student("kumar",
21);
Student s2 = new Student("Raj",
20);
s1.display();
s2.display();
}
}
Output
kumar 21
Raj 20
3. Copy Constructor in Java
The Java does not provide a built-in copy
constructor like C++ but we can create one manually. A copy constructor copies
data from one object to another.
Example :
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
Student(Student s) {
name = s.name;
age = s.age;
}
void display() {
System.out.println(name + "
" + age);
}
public static void main(String[]
args) {
Student s1 = new Student("kumar",
22);
Student s2 = new Student(s1);
s2.display();
}
}
Output
kumar 22
Constructor Overloading
The Constructor overloading means creating
multiple constructors in the same class with different parameter lists.
Example :
class Test {
Test() {
System.out.println("Default
Constructor");
}
Test(int a) {
System.out.println(a);
}
Test(String name) {
System.out.println(name);
}
public static void main(String[]
args) {
Test t1 = new Test();
Test t2 = new Test(100);
Test t3 = new
Test("Java");
}
}
Output
Default Constructor
100
Java
Rules of Constructors in Java
1. Constructor Name Must Match Class
Name
class Test {
Test() {
}
}
2. Constructors Do Not Have Return Types
Wrong:
void Test() {
}
Correct:
Test() {
}
3. Constructors Cannot Be Static
Wrong:
static Test() {
}
4. Constructors Can Be Overloaded
The Java allows multiple constructors with
different parameters.
5. Constructors Execute Automatically
Student s1 = new Student();
The constructor executes immediately after object creation.
Frequently Asked Interview Questions
Q1. Can constructors be inherited?
No, constructors cannot be inherited.
Q2. Can constructors be overloaded?
Yes, Java supports constructor overloading.
Q3. Can constructors be private?
Yes, private constructors are allowed.
Q4. Can constructors be static?
No, constructors cannot be static.
Q5. What happens if no constructor is
created?
The Java automatically provides a default
constructor.
Q6. Can a constructor call another
constructor?
Yes, using this().
Q7. Can constructors return values?
No, constructors do not return values.
Conclusion
The Constructors are one of the core
building blocks of Java programming. They are used to initialize objects and
simplify object creation. Understanding constructors is extremely important for
mastering object-oriented programming concepts in Java.
The Java provides different types of
constructors such as default, parameterized & copy constructors. A strong
understanding of constructors helps developers write cleaner & more
efficient Java applications.