this Keyword and Constructor Overloading? - java cource

Learn Java this keyword and constructor overloading with examples. Day 18 Java tutorial for beginners.
The this Keyword

On Day 18, we explore the this keyword and constructor overloading. These are essential concepts in Java OOP for writing clean and flexible code.

1. The this Keyword

The this keyword refers to the current object. It is often used to differentiate between instance variables (fields) and method/constructor parameters with the same name.


class Student {
    String name;
    int age;

    // parameterized constructor using this
    Student(String name, int age) {
        this.name = name; // 'this.name' refers to the field
        this.age = age;   // 'age' refers to parameter
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Alice", 20);
        System.out.println(s1.name + " - " + s1.age);
    }
}

Explanation: Without this, Java would confuse between the constructor parameter and the field. this.name means the object's field, while name is the constructor parameter.

2. Constructor Overloading

A class can have multiple constructors with different parameter lists. This is called constructor overloading.


class Student {
    String name;
    int age;

    // default constructor
    Student() {
        this.name = "Unknown";
        this.age = 0;
    }

    // parameterized constructor
    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();               // calls default
        Student s2 = new Student("Bob", 22);      // calls parameterized

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

Explanation: When creating an object, Java decides which constructor to call based on the arguments provided.

3. Calling One Constructor from Another (this())

You can call one constructor from another in the same class using this().


class Student {
    String name;
    int age;

    // constructor 1
    Student() {
        this("Unknown", 0); // calls constructor 2
    }

    // constructor 2
    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();          // uses constructor 1
        Student s2 = new Student("Charlie", 21); // uses constructor 2

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

Explanation: this() avoids repeating initialization code. It must always be the first line inside a constructor.

Summary

The this keyword resolves naming conflicts and refers to the current object. Constructor overloading allows multiple ways to create objects, and this() helps reuse initialization logic.

Post a Comment