Constructors in Java (Default & Parameterized)

(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 variables
  • name, age β†’ Object variables

Assignment

name = n;
age = a;

πŸ‘‰ Data flows from parameters β†’ object variables


πŸ” Default vs Parameterized

FeatureDefault ConstructorParameterized Constructor
ParametersNoneYes
ValuesFixedCustom
FlexibilityLowHigh

πŸ§ͺ 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 βœ…

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *