Java Keywords (Part XV): The many uses of the this
keyword
Java keyword list
abstract | continue | for | new | switch |
assert | default | goto* | package | synchronized |
boolean | do | if | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient |
catch | extends | int | short | try |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const* | float | native | super | while |
Using this()
to chain constructor calls
public class MyClass {
public MyClass() {
System.out.println("From no-arg constructor");
}
public MyClass(String s) {
this();
System.out.println("From one-arg constructor");
}
}
this()
appears before the System.out.println()
call. When using this
to invoke a call to another constructor in the class, it must be the very first line inside the constructor. Otherwise, it will result in a compiling error. Using this
to call another constructor in the class is called explicit constructor invocation.
The second thing to know is that you can call this()
with arguments so long there is a constructor in the class that has the same number and type of arguments. Typically, your no-argument constructor is the base constructor. Meaning that the no-argument constructor will not include a call to another constructor using this
. Two constructors calling each other using this
will result in an endless recursive call. Fortunately, the compiler will not allow it; therefore resulting in a compiler error.
Using this()
to reference variables
public class MyClass {
private String name;
public MyClass(String name) {
this.name = name;
}
}
this
are the ones that belong to the instance being constructed.
Using this
to reference the object itself
This is perhaps the least used and, in my opinion, not recommended in most cases. Consider the following example:
public class MyClass {
private String s = "bar";
public static void main(String[] args) {
MyClass cc1 = new MyClass();
MyClass cc2 = cc1.someMethod();
cc1.setS("foo"); // What will this do to object cc2?
System.out.println(cc1.equals(cc2));
}
public void setS(String s) {
this.s = s;
}
public MyClass someMethod() {
return this; // You must understand the impact
}
}
true
100% of the time. The reason is very simple. The method someMethod()
returns itself. Please understand that I did not mean a copy of itself. The object cc1
is returning the address of itself to object cc2
. Because both objects are pointing to the same address in memory, changes made to one object automatically affect the other. Most of the time, creating duplicate objects in this manner is wasteful and unnecessary. But, the language allows it becuase they might be some situations when this is warranted. It is up to you to decide. But, as many other things in software development, make sure you understand the impact of what you are going to do before you do it.
I hope this was of some benefit to you. Next up, is using the keyword super
.
Comments
Post a Comment