Java Programming Day 9

HimanshiHimanshi
5 min read

🌞 Java Day 9: Constructors and Constructor Overloading – My Aha Moment!

Hey blog! 👋
So today was surprisingly fun and a little “mind-blowing” in terms of concepts. I finally got my head around constructors and constructor overloading in Java. And no, it's not as scary as it sounds! Let me take you through everything I learned—with examples, doubts I had, and all the mini “ohhh I get it now” moments.


❓ First off, what is a constructor?

When I first heard the word constructor, I thought:

"Umm… is it like a function that builds something?"

Well, sort of. Here's the proper explanation:

A constructor is a special method in Java that is used to initialize objects of a class. It has the same name as the class and it doesn’t have a return type—not even void.

And here's the magic: It automatically gets called when you create an object!
So yeah, you don’t even need to call it like a regular method. ✨


🧠 Doubt I had:

“If we already have methods, why do we even need constructors?”

Good question, past me! 😂
The answer is:

  • Methods perform actions

  • Constructors set up the initial state of an object
    That means, when you create a new object, you can already decide its name, age, email, etc. right at that moment!


📌 My First Program: The Student Constructor

Here’s the class and what I wrote:

javaCopyEditpublic class Student {
    String name;
    int age;
    double gpa;
    boolean isEnrolled;

    // Constructor to initialize the object
    Student(String name, int age, double gpa) {
        this.name = name;
        this.age = age;
        this.gpa = gpa;
        this.isEnrolled = true;
    }

    void study() {
        System.out.println(name + " is currently studying 📖");
    }
}

Now in main, I created the object like this:

javaCopyEditStudent s1 = new Student("Himanshi", 18, 9.1);
s1.study();

Output:

csharpCopyEditHimanshi is currently studying 📖

And that’s when it clicked. Constructors give default values right at object creation!


🤯 Then I met: Constructor Overloading

Now things got more fun. I realized that you can have multiple constructors in the same class—as long as they have different parameters.


💻 Program 2: The Overloaded Constructor

Class Name: UserProfile

javaCopyEditpublic class UserProfile {
    String username;
    String email;
    int age;

    // Default constructor
    UserProfile() {
        this.username = "Guest";
        this.email = "Not provided";
        this.age = 0;
    }

    // Constructor with only username
    UserProfile(String username) {
        this.username = username;
        this.email = "Not provided";
        this.age = 0;
    }

    // Constructor with username and email
    UserProfile(String username, String email) {
        this.username = username;
        this.email = email;
        this.age = 0;
    }

    // Constructor with all fields
    UserProfile(String username, String email, int age) {
        this.username = username;
        this.email = email;
        this.age = age;
    }
}

🧪 And the demo class:

javaCopyEditpublic class OverloadedConstructorDemo {
    public static void main(String[] args) {
        UserProfile user1 = new UserProfile("Spongebob");
        System.out.println("User 1 → " + user1.username + ", " + user1.email + ", Age: " + user1.age);

        UserProfile user2 = new UserProfile("Patrick", "patrick123@gmail.com");
        System.out.println("User 2 → " + user2.username + ", " + user2.email + ", Age: " + user2.age);

        UserProfile user3 = new UserProfile("Sandy", "sandy123@gmail.com", 25);
        System.out.println("User 3 → " + user3.username + ", " + user3.email + ", Age: " + user3.age);

        UserProfile user4 = new UserProfile();
        System.out.println("Guest User → " + user4.username + ", " + user4.email + ", Age: " + user4.age);
    }
}

Output:

sqlCopyEditUser 1 → Spongebob, Not provided, Age: 0
User 2 → Patrick, patrick123@gmail.com, Age: 0
User 3 → Sandy, sandy123@gmail.com, Age: 25
Guest User → Guest, Not provided, Age: 0

💬 What did I realize?

This is super helpful when:

  • You don’t have full info yet — use a simple constructor

  • You want to provide full details — use the full constructor

  • You want to just test the object — use the default one

It gives me so much flexibility.


⚡ Program 3 Recap (with simple print statements):

javaCopyEdit// Creating objects in multiple ways
UserProfile u1 = new UserProfile("Aditi");
UserProfile u2 = new UserProfile("Riya", "riya@gmail.com");
UserProfile u3 = new UserProfile("Megha", "megha@gmail.com", 20);
UserProfile u4 = new UserProfile();

System.out.println(u1.username + " | " + u1.email + " | " + u1.age);
System.out.println(u2.username + " | " + u2.email + " | " + u2.age);
System.out.println(u3.username + " | " + u3.email + " | " + u3.age);
System.out.println(u4.username + " | " + u4.email + " | " + u4.age);

👀 What were my key takeaways today?

  • Constructors automatically get called when we create an object.

  • They’re used to initialize the values of attributes.

  • Constructor Overloading = multiple ways to create objects.

  • You can give default values, partial values, or all values at the time of object creation.

  • Super useful for making flexible, user-friendly classes.


🧠 Questions I asked myself and answered today:

  • ❓ Can a class have multiple constructors?
    ✅ Yes, if they have different parameter lists.

  • ❓ What happens if I don’t write a constructor?
    ✅ Java provides a default no-argument constructor.

  • ❓ Can constructors be inherited?
    ❌ No, constructors are not inherited like methods.

  • ❓ Can I call one constructor from another?
    ✅ Yes, using this() keyword inside constructors (will learn that next!).


🏁 Summary:

Constructors are like the entry gate to your object’s data.
Overloading them is like giving the user multiple types of tickets to enter—each with different details!


That’s it for today, blog! 💻
Tomorrow, I'm diving into this() vs super(), and how to chain constructors.
Until then, happy Java-ing! ☕💫

1
Subscribe to my newsletter

Read articles from Himanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshi
Himanshi

Hi! I'm a curious and self-driven programmer currently pursuing my BCA 🎓 and diving deep into the world of Java ☕ from Day 1. I already have a foundation in programming, and now I'm expanding my skills one concept, one blog, and one project at a time. I’m learning Java through Bro Code’s YouTube tutorials, experimenting with code, and documenting everything I understand — from basic syntax to real-world applications — to help others who are just starting out too. I believe in learning in public, progress over perfection, and growing with community support. You’ll find beginner-friendly Java breakdowns, hands-on code snippets, and insights from my daily coding grind right here. 💡 Interests: Java, Web Dev, Frontend Experiments, AI-curiosity, Writing & Sharing Knowledge 🛠️ Tools: Java • HTML/CSS • JavaScript • Python (basics) • SQL 🎯 Goal: To become a confident Full Stack Developer & AI Explorer 📍 Based in India | Blogging my dev journey #JavaJourney #100DaysOfCode #CodeNewbie #LearnWithMe