(Based on your video content)
π Introduction
In this article, we will understand one of the most important concepts in Java OOP:
π Constructors
We will learn:
- What is a constructor
- Why constructors are needed
- Problems without constructors
- Default constructor
- Parameterized constructor
- Practical examples
β The Problem: Empty Object Issue
In previous examples, we created objects like this:
Student s1 = new Student();
π But there is a problemβ¦
- The object is created
- But data is NOT initialized
β οΈ What Happens Next?
We manually assign values:
s1.name = "Alice";
s1.age = 20;
s1.grade = 'A';
π¨ Issues with This Approach
- Too many lines of code
- Easy to forget a variable
- Increases chances of errors
- Reduces code readability
- Makes program messy
β Solution: Constructors
π A constructor helps to:
- Automatically initialize object data
- Reduce code lines
- Make code cleaner and faster
π₯ Example (Before vs After)
β Without Constructor
Student s1 = new Student();
s1.name = "Alice";
s1.age = 20;
β With Constructor
Student s1 = new Student("Alice", 20);
π One line instead of multiple lines!
π§ What is a Constructor?
A constructor is:
π A special method inside a class that:
- Runs automatically when object is created
- Initializes object data
π Key Rules
- Name must be same as class
- No return type (not even
void) - Called automatically using
new - Cannot be called manually
ποΈ Anatomy of a Constructor
public Student() {
// constructor body
}
πΉ Types of Constructors
1οΈβ£ Default Constructor
π Definition
- Takes no parameters
- Assigns default values
β Example
class Student {
String name;
int age; Student() {
name = "Unknown";
age = 0;
}
}
π‘ Key Points
- Called automatically
- Useful for safe default values
- Prevents null or garbage data
π₯οΈ Usage
Student s1 = new Student();
π Output will use:
- name = “Unknown”
- age = 0
2οΈβ£ Parameterized Constructor
π Definition
- Takes parameters
- Assigns custom values
β Example
class Student {
String name;
int age; Student(String n, int a) {
name = n;
age = a;
}
}
π‘ Key Points
- Accepts input values
- Allows creating customized objects
- More flexible than default constructor
π₯οΈ Usage
Student s2 = new Student("Ajay", 25);
π Understanding Variables
Inside constructor:
Student(String n, int a)
n,aβ Local variablesname,ageβ Object variables
Assignment
name = n;
age = a;
π Data flows from parameters β object variables
π Default vs Parameterized
| Feature | Default Constructor | Parameterized Constructor |
|---|---|---|
| Parameters | None | Yes |
| Values | Fixed | Custom |
| Flexibility | Low | High |
π§ͺ Practical Example (Complete Code)
class Student {
String name;
int age; // Default Constructor
Student() {
name = "";
age = 0;
} // Parameterized Constructor
Student(String n, int a) {
name = n;
age = a;
} void display() {
System.out.println(name + " " + age);
}
}
π₯οΈ Main Class
class Main {
public static void main(String[] args) { Student s1 = new Student(); // Default
Student s2 = new Student("Ajay", 25); // Parameterized s1.display();
s2.display();
}
}
π¨οΈ Output
0
Ajay 25
β οΈ Common Mistakes
β Giving Return Type
void Student() { } // WRONG
π Constructors must NOT have return type
β Trying to Call Constructor Manually
s1.Student(); // WRONG
π Constructor is called automatically
β Forgetting Parameters
new Student(); // but only parameterized constructor exists
π Causes error
π₯ Key Takeaways
- Constructor = automatic initialization
- Runs when object is created
- Makes code cleaner and shorter
- Two main types:
- Default
- Parameterized
π§ Final Insight
π Without constructor:
- Code = messy β
π With constructor:
- Code = clean, efficient, readable β