Unit 3 · OOP Concepts

3.1.4The this keyword and constructors

The this keyword

this refers to the current object: the object whose method or constructor is running. It is used to separate an attribute from a parameter with the same name, as shown in the constructor below.

Why is it needed? Inside the constructor below, the parameter name and the attribute name have the same name. Java always uses the nearest declaration, so a plain name means the parameter: the parameter shadows the attribute. this.name means "the name attribute of this object". Without this, name = name; would just copy the parameter to itself, and the attribute would stay null.

this has three common uses:

  1. To tell an attribute from a parameter: this.name = name;
  2. To call another constructor of the same class: this("Unknown", 0); (see below).
  3. To pass the current object to another method: for example library.register(this); inside a Member method.

Constructors

A constructor is a special method that runs automatically when an object is created. It initialises the attributes.

Rules: the name equals the class name; it has no return type; it can take parameters.

Java
public class Student {
    String name;
    int marks;

    // parameterised constructor
    Student(String name, int marks) {
        this.name = name;
        this.marks = marks;
    }

    // default (no-argument) constructor
    Student() {
        this.name = "Unknown";
        this.marks = 0;
    }
}
Java
Student a = new Student("Nimal", 80);
Student b = new Student();            // Unknown, 0

If you write no constructor, Java supplies an empty default constructor. As soon as you write any constructor, Java no longer supplies the default one.

Copy constructor: a constructor that builds an object from another object of the same class.

Java
Student(Student other) {
    this.name = other.name;
    this.marks = other.marks;
}

What a constructor really does

When Java runs new Student("Nimal", 80):

  1. Memory for a new Student object is set aside on the heap, and every attribute gets its default value (null and 0).
  2. The matching constructor runs: the one whose parameter list matches the arguments (a String and an int).
  3. The constructor's body sets name to "Nimal" and marks to 80.
  4. new gives back a reference to the finished object, which is stored in the variable.

A constructor guarantees that an object starts in a valid state. Without one, every programmer who creates a Student must remember to set every attribute; with one, they cannot forget.

Constructor chaining with this(...)

When several constructors share work, one can call another with this(...). This avoids repeating code:

Java
Student(String name, int marks) {
    this.name = name;
    this.marks = marks;
}

Student() {
    this("Unknown", 0);    // reuse the two-parameter constructor
}

this(...) must be the first statement in the constructor. Now any check you add later (for example, that marks is between 0 and 100) only has to be written once.

The copy constructor explained

new Student(a) creates a new, separate object with the same values as a. Compare this with Student b = a;, which creates no new object at all. With the copy constructor, changing b.marks afterwards does not affect a.

Common mistake: writing a return type on a constructor, such as void Student() { ... }. The compiler accepts it, but it is now an ordinary method named Student, not a constructor, so it never runs when an object is created.

Common mistake: writing only Student(String name, int marks) and then trying new Student(). This fails to compile, because once you write any constructor Java no longer supplies the default one. Add a no-argument constructor yourself if you need it.

Remember

  • A constructor has the class's name and no return type; it runs once, when new creates the object.
  • this means the current object; this.name is the attribute, plain name is the parameter.
  • this(...) calls another constructor of the same class and must come first.