Java Lesson 18 – Constructors | Dataplexa

Constructors in Java

When we create objects in Java, we often want them to start with meaningful values instead of empty or default data.

This is where constructors come into the picture. They help prepare an object the moment it is created.


What Is a Constructor?

A constructor is a special method in a class that is automatically executed when an object is created.

Its main purpose is to initialize the object — that means setting initial values for variables.

Unlike normal methods, a constructor:

  • Has the same name as the class
  • Does not have a return type
  • Runs automatically when an object is created

Why Constructors Are Important

Without constructors, every object starts with default values like 0, null, or false.

In real applications, that is rarely useful. We want objects to start in a valid and meaningful state.

Constructors make sure an object is ready to use immediately.


Example Without a Constructor

Let’s first see what happens when we do not use a constructor.

class Student {
    String name;
    int age;
}

public class Demo {
    public static void main(String[] args) {

        Student s = new Student();
        System.out.println(s.name);
        System.out.println(s.age);
    }
}

The output will be null and 0, because nothing was initialized.


Default Constructor

If you do not create any constructor, Java automatically provides a default constructor.

It simply creates the object without setting custom values.

However, in real projects, we usually want more control.


Creating Your Own Constructor

Let’s improve the student class by adding a constructor.

class Student {

    String name;
    int age;

    Student(String n, int a) {
        name = n;
        age = a;
    }
}

This constructor accepts values and assigns them when the object is created.


Using the Constructor

Now let’s create objects using the constructor.

public class Demo {

    public static void main(String[] args) {

        Student s1 = new Student("Alice", 20);
        Student s2 = new Student("Bob", 22);

        System.out.println(s1.name + " - " + s1.age);
        System.out.println(s2.name + " - " + s2.age);
    }
}

Each object is created with its own values, making the program cleaner and safer.


Real-World Analogy

Think of buying a new mobile phone.

When you turn it on for the first time, it already has:

  • A language
  • Date and time
  • Basic system settings

Those initial settings are like a constructor — they prepare the object before you start using it.


Multiple Constructors

A class can have more than one constructor, as long as their parameters are different.

This allows flexibility when creating objects.

You will explore this concept deeper when learning constructor overloading.


What You Learned in This Lesson

  • What constructors are
  • Why constructors are needed
  • How to create and use constructors
  • How constructors improve object initialization

In the next lesson, you will learn about inheritance, which allows one class to reuse another class’s behavior.