Before We Begin
Why do we create instances? That is, we execute code like new ClassName() to allocate to memory and execute
member fields or methods of that instance through a reference pointing to the created instance. Like below:
class MadPlay {
String name;
public void sayHi() {
System.out.println("Hi!")
}
public static void main(String[] args) {
MadPlay ref = new MadPlay();
ref.sayHi();
}
}
Breaking down the above code one by one. First, we defined a class called MadPlay. And this class has name of String type.
And there’s a method called sayHi that simply outputs a string.
Next, looking at the main method, the start of Java code. We created an object (instance) of the MadPlay class and put it in a reference named ref. That is, here reference ref points to the created object of the MadPlay class. And through this, we’re calling instance members (methods). As such, class members cannot be used without creating an object of that class. That is, instance members are only used through instances.
this
When developing Java programs, you’ll eventually encounter the this keyword. It’s a keyword that points to the object itself, and to explain more,
it refers to a reference to an object belonging to the currently executing context. In some cases, it’s automatically generated by the compiler,
so it’s not necessarily required. But the following case is different. What happens when the class’s member variable name and the mutator (setter)’s
parameter name are the same?
class Person {
String name;
public void setName(String name) {
this.name = name // !?
}
public String getName() {
return name;
}
// ...
}
public static void main(String[] args) {
Person p = new Person();
p.setName("MadPlay");
}
What happens if we omit the this keyword in the mutator (setter) above? You might expect it to assign well on its own, but that’s not the case. You may encounter
a warning message saying it’s assigning a value to itself. Due to variable locality, both names refer to local variables.
Therefore, this can be used to clearly indicate that it’s a member field.
On the other hand, we didn’t use this in the accessor (getter). There are no parameters, and within the block where the accessor method is performed, there are no variables with the same name. Therefore, name within the accessor is clearly a member field.
this and Constructors
this can also be used with constructors. It’s used when calling your own constructor.
public class Person {
private String name;
private String email;
private String phoneNumber;
public Person(String name) {
this(name, null);
}
public Person(String name, String email) {
this(name, email, null);
}
public Person(String name, String email, String phoneNumber) {
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
}
}
The point to note here is that it must be located on the very first line of the constructor. Like this, this() like method calls
can only be used in constructors.
this and Self-Return
As mentioned earlier, this is a keyword that points to the object itself. Using this, you can return a reference to yourself.
Modifying the this and constructors code we made earlier a bit. Here, we’ll use the builder pattern.
public class Person {
private String name;
private String email;
private String phoneNumber;
public static class Builder {
private String name;
private String email;
private String phoneNumber;
// Constructor
public Builder(String name) {
this.name = name;
}
public Builder email(String value) {
email = value;
return this; // Return self
}
public Builder phoneNumber(String value) {
phoneNumber = value;
return this; // Return self
}
public Person build() {
return new Person(this);
}
}
private Person(Builder builder) {
name = builder.name;
email = builder.email;
phoneNumber = builder.phoneNumber;
}
}
We applied the builder pattern using the fact that methods can return themselves. When constructors have many parameters, applying
the builder pattern using this like this is also good.
this and Static Methods
The this keyword is defined as a reference to your own object dynamically, that is, during execution.
Therefore, it cannot be used in static methods.
class Person {
private String name;
public static void someMethod(String name) {
// Cannot use this in a static context error.
this.name = name;
}
}