Java this Keyword
›Short versionOpen if you only have a minute.
- this is the current instance: the object the method or constructor was invoked on.
- A parameter with the same name as a field hides the field. this.field reaches the field.
- this(...) calls another constructor in the same class and must be the first statement.
- A static context has no this. The call has to name an object before it can use instance state.
- return this gives the caller the same object back, which is how a short chain of calls stays on one account.
- Passing this to another object publishes the reference. Finish initialization before you do that.
Two accounts sit on the counter. You point at one and say "deposit twenty." The pointing is the receiver. Inside the method, is how the code keeps holding the account you pointed at, the .
When the names collide
Constructors often use the field name as the parameter name, because the parameter is the field's incoming value. That is . The parameter wins until you qualify the field.
public class Account {
private final String id;
private int balance;
public Account(String id, int balance) {
this.id = id;
this.balance = balance;
}
}this.id = id stores the parameter into the field. id = id does not. Both sides are the parameter, the field stays null, and the constructor looks as if it worked. Use this whenever a local name hides a field. When nothing hides the field, the bare name is enough and easier to read.
Sending the work to another constructor
is not the same token used as a receiver. It is a constructor call, and it has to come first.
public Account(String id) {
this(id, 0);
}
public Account(String id, int balance) {
this.id = id;
this.balance = balance;
}The one-argument constructor never assigns the fields itself. It asks the two-argument constructor to do that, then it would continue with any statements after this(...). You cannot write super(...) in the same constructor. One of those two calls occupies the first line, and the other constructor in the chain reaches the superclass.
Handing the same object back
A method returns the receiver. The next call in the chain uses that returned reference, which is still the original account.
public Account deposit(int amount) {
balance += amount;
return this;
}
// savings.deposit(20).deposit(5);Passing this outward is the same reference, and the receiver may use it at once. A constructor that calls registry.add(this), starts a thread, or creates an inner instance and stores it somewhere reachable, lets other code see the account before construction returns. If that line runs in a superclass constructor, the subclass initializers have not run, so subclass fields are still 0, false, or null. The same early look happens when the superclass constructor calls a method the subclass overrides. Assign the fields, let new return, and only then register the object.
$ javac Account.java$ java Account
The constructor parameter is named balance, and the field is balance. What does balance = balance store?
It stores the parameter into the parameter. The field is hidden, so it stays at the default, 0 for an int. this.balance = balance is the assignment that writes the field of the current instance.