Constructors in Java: Birth Certificates for Your Objects
Rodrigo De Lascio

Rodrigo De Lascio @rodrigodelascio

About: Full-stack developer who took the scenic route: content, design, localisation - been there, rocked that. Now I just make computers do what I say (mostly).

Location:
Walton-on-Thames, UK
Joined:
Mar 8, 2024

Constructors in Java: Birth Certificates for Your Objects

Publish Date: Jun 13
0 0

This week we are back on track with actual code. I have been learning about constructors, which is Java’s way of saying: let’s give your object a proper birth certificate.

If classes are the blueprints of your objects, then constructors are the part where you actually build them, assign some identity, and hand them their very first onesie.


What Even Is a Constructor?

A constructor is a special method that runs automatically whenever you create an object from a class. It is like the delivery room for your object. You call new, and the constructor makes sure everything is initialised and ready to go.

public class Fighter {
    private String name;
    private String nationality;

    public Fighter(String name, String nationality) {
        this.name = name;
        this.nationality = nationality;
    }
}

Enter fullscreen mode Exit fullscreen mode

Every time I create a Fighter, I am forced to provide a name and nationality. No object leaves the hospital incomplete.
Fighter f1 = new Fighter("Kimberly Maria", "Brazilian");

Now we have a matured fighter, born with valid data, ready to punch through some logic.

Why Constructors Help Keep Things Clean

Before I understood constructors properly, I was writing things like this:

Fighter f1 = new Fighter();
f1.setName("Kimberly Maria");
f1.setNationality("Brazilian");
Enter fullscreen mode Exit fullscreen mode

It works, but it is risky. What if I forget to set one of the fields?

What if I only call setName but forget setNationality?

Now you have an incomplete object wandering around, awkwardly standing in the code, missing half its identity.

Constructors prevent that by forcing you to provide essential data upfront.

Default vs Custom Constructors

By default, Java gives you an empty constructor if you do not declare one.

However, once you create your own constructor, Java politely steps back and says,

"Okay mate, you are on your own now."

If you still want to allow creating empty objects, you can keep both:

public Fighter() {
    // default constructor
}

public Fighter(String name, String nationality) {
    this.name = name;
    this.nationality = nationality;
}
Enter fullscreen mode Exit fullscreen mode

This way you have options. Both full custom births and empty templates, depending on your needs.

Bonus: Overloading Constructors

Because Java is quite polite and mildly obsessive, it allows you to create multiple constructors with different parameters, as long as their parameter lists differ:

public Fighter(String name) {
    this.name = name;
    this.nationality = "Unknown";
}
Enter fullscreen mode Exit fullscreen mode

Now if you are too lazy to type both name and nationality every time (we have all been there), you have options.

Final Thoughts

Constructors are one of those simple but powerful concepts in Java that make your code cleaner, more predictable, and more professional.

They give your objects proper identity from the moment they exist. No half-born variables lying around hoping you will remember to feed them later.

For a career-changer like me, every new piece of Java I grasp like this feels less like studying and more like collecting small wins.
Next up: inheritance, or as I like to call it, Java’s family drama.

Comments 0 total

    Add comment