🔷 What is a Constructor in Java?
A constructor is a special method that:
✔ Has the same name as the class
✔ Is used to initialize objects
✔ Runs automatically when object is created
✔ Does NOT have a return type (not even void)
🔷 Why Do We Need a Constructor?
When we create an object, we often want to:
Set initial values
Assign default data
Initialize variables
Constructor does this job.
🔷 Basic Example
class Student {
Student() { // Constructor
[Link]("Constructor is called");
}
public static void main(String[] args) {
Student s1 = new Student(); // Constructor runs here
}
}
🔎 Output:
Constructor is called
👉 Constructor runs automatically when object is created.
🔹 1️⃣ Default Constructor
✅ What is it?
It is provided automatically by Java.
It is created when you do not write any constructor.
It initializes variables with default values (0, null, false).
✅ Example:
class Car {
int speed;
public static void main(String[] args) {
Car c1 = new Car(); // Default constructor is used
[Link]("Speed: " + [Link]);
}
}
🔎 Output:
Speed: 0
Explanation:
We did not write any constructor.
Java automatically created a default constructor.
int speed gets default value 0.
🔹 2️⃣ No-Argument Constructor (User
Defined)
✅ What is it?
Created by programmer.
Does not take any parameters.
Used to assign default values manually.
✅ Example:
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 18;
}
void display() {
[Link](name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
[Link]();
}
}
🔎 Output:
Unknown 18
Explanation:
Constructor sets custom default values.
Runs automatically when object is created.
🔹 3️⃣ Parameterized Constructor
✅ What is it?
Takes parameters.
Used to give different values to different objects.
✅ Example:
class Employee {
String name;
int salary;
Employee(String n, int s) {
name = n;
salary = s;
}
void show() {
[Link](name + " " + salary);
}
public static void main(String[] args) {
Employee e1 = new Employee("Ali", 30000);
Employee e2 = new Employee("Rahul", 50000);
[Link]();
[Link]();
}
}
🔎 Output:
Ali 30000
Rahul 50000
Explanation:
Values are passed while creating object.
Each object can have different data.
🔷 What is a Copy Constructor?
A copy constructor is a constructor that:
✔ Creates a new object
✔ Copies data from another object
✔ Takes an object of same class as parameter
🔷 Important Point
Java does NOT provide built-in copy constructor like C++.
We create it manually.
🔷 Syntax of Copy Constructor
ClassName(ClassName obj) {
// copy data from obj
}
✅ Simple Example
class Student {
String name;
int age;
// Parameterized Constructor
Student(String n, int a) {
name = n;
age = a;
}
// Copy Constructor
Student(Student s) {
name = [Link];
age = [Link];
}
void display() {
[Link](name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Ali", 20);
// Copying object
Student s2 = new Student(s1);
[Link]();
[Link]();
}
}
🔎 Output:
Ali 20
Ali 20
🔷 How It Works
1. s1 is created with values "Ali" and 20
2. s2 is created by copying s1
3. Copy constructor copies values from s1 into s2