Constructors and the `this` Keyword

These concepts are crucial for object creation and understanding how to manage object state properly.


πŸ—οΈ 1. What is a Constructor?

βœ… Definition:

A constructor in Java is a special method that is automatically called when an object is created. It is used to initialize objects.

πŸ”§ Syntax:

ClassName() {
    // initialization code
}

πŸ” Key Rules:

  • Constructor has no return type (not even void)
  • The name of the constructor must match the class name
  • It can be parameterized (to initialize values) or default (no parameters)

βœ… Example: Default Constructor

public class Car {
    String brand;

    // Default constructor
    Car() {
        brand = "Tesla";
    }

    void showBrand() {
        System.out.println("Brand: " + brand);
    }
}
public class Main {
    public static void main(String[] args) {
        Car c = new Car();   // constructor called automatically
        c.showBrand();       // Output: Brand: Tesla
    }
}

βœ… Example: Parameterized Constructor

public class Car {
    String brand;
    int speed;

    // Parameterized constructor
    Car(String brand, int speed) {
        this.brand = brand;
        this.speed = speed;
    }

    void showDetails() {
        System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");
    }
}
public class Main {
    public static void main(String[] args) {
        Car car1 = new Car("Toyota", 120);
        Car car2 = new Car("BMW", 180);

        car1.showDetails(); // Output: Brand: Toyota, Speed: 120 km/h
        car2.showDetails(); // Output: Brand: BMW, Speed: 180 km/h
    }
}

πŸ” 2. The this Keyword

βœ… Definition:

this is a reference to the current object inside a class. It is used to:

  • Refer to current object’s fields
  • Invoke current class methods or constructors
  • Pass the current object as a parameter

🧠 Why use this?

When parameter names and field names are the same, this helps distinguish between them.


πŸ”§ Example:

public class Student {
    String name;
    int age;

    Student(String name, int age) {
        this.name = name; // distinguishes between parameter and instance variable
        this.age = age;
    }

    void show() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Alice", 22);
        s1.show();  // Output: Name: Alice, Age: 22
    }
}

πŸ”„ Constructor Overloading

Just like method overloading, Java allows multiple constructors in the same class with different parameters.

πŸ”§ Example:

public class Book {
    String title;
    int pages;

    // Default constructor
    Book() {
        this.title = "Untitled";
        this.pages = 0;
    }

    // Parameterized constructor
    Book(String title, int pages) {
        this.title = title;
        this.pages = pages;
    }

    void info() {
        System.out.println("Title: " + title + ", Pages: " + pages);
    }
}

🧠 When to Use this() Constructor Call

You can use this() to call another constructor inside the same class.

public class Book {
    String title;
    int pages;

    Book() {
        this("Unknown", 0);  // Calls the parameterized constructor
    }

    Book(String title, int pages) {
        this.title = title;
        this.pages = pages;
    }

    void info() {
        System.out.println("Title: " + title + ", Pages: " + pages);
    }
}

πŸ“ Summary Table

ConceptUse
ConstructorInitializes objects
Default ConstructorNo parameters, sets default values
ParameterizedSets values via arguments
Constructor OverloadingMultiple ways to initialize
this keywordRefers to current object or constructor

πŸŽ“ Homework for Students

  1. Create a Laptop class with fields:

    • brand (String)
    • ram (int)
    • Constructors:
      • Default constructor β†’ sets brand = “Dell”, ram = 8
      • Parameterized constructor β†’ sets custom values
    • Method: display() to print details
  2. Create 2 objects in main:

    • One using the default constructor
    • One using the parameterized constructor
  3. Bonus: Use this() to call one constructor from the other.


Would you like the next topic to be:

  • Static Keyword & Static Blocks
  • Method Overloading vs Overriding
  • Access Modifiers (public, private, etc.)

You’re steering the ship, coach πŸš’πŸ‘¨β€πŸ«