# (Java) I don't get how ".this" works?

I am learning classes and objects, and I don't get how ".this" works in Java. I don't know what it is used for and how to use it.

My book that I am learning from doesn't do a good job of explaining how ".this" works. The book is: Java The Complete Reference 9th Edition.

Related Programming and Computer Science News on Phys.org
this={current class and its parent classes}
e.g
PHP:
public class A {public void methodA(){}}
public class B extends A
{
public void methodB()
{
this.methodA();
this.obj=0;
}
private int obj;
}

this={current class and its parent classes}
e.g
PHP:
public class A {public void methodA(){}}
public class B extends A
{
public void methodB()
{
this.methodA();
this.obj=0;
}
private int obj;
}
Can you explain what it does and what the applications are?

"this" acts like a "self" reference. It's good to be there to reference non-static class members (objects and methods) ("static" things are not made for "this").
For clarity purpose
PHP:
public class B
{
public void methodB(int obj)
{
this.methodA();
this.obj=obj; // argument name and class member are of the same name, "this" must be used
}
private int obj;
}
Other than that I don't know when "this" must be used.

So basically you use it when two variables are of the same name? But can't you use two different names? If you use it more than once (on the same variable name), will it work?

Borg
Gold Member
Routaran
So basically you use it when two variables are of the same name?
It is not the only reason, but yes. Also, as others said, when your code gets very large, someone (or even you) can see which variable belongs to the working object instead of scrolling all the way to whatever it is the declaration of such variable to understand what is going on.
But can't you use two different names?
You can, but things get really annoying when you have lots of classes and lots of variables within such classes.

Say you have 5 variables within an object constructor's arguments. If we go with your method, we need 10 different names. And it is annoying to write 10 different names not only in the constructor's argument, but also within the constructor's code to initialize the variables.

Medicol
How will you know which variable is which when you use it more than once?

How will you know which variable is which when you use it more than once?
Um... I thought this was clear, but anyways... Precisely with the use of "this" keyword.

If you use the word "this" before the variable, you know it is from your object. Otherwise it is from your method.

Following Silicon Waffle's example of writing examples:
Java:
public class SimpleSymetricKey {

private byte[] myKey;

//Constructor
public SimpleSymetricKey(byte[] myKey) {
this.myKey = myKey;
}
}
As you can see the left side of the assignment (=) contains the keyword this while the right side doesn't. Therefore, the left side of the assignment (=) refers to the variable of the SimpleSymetricKey object and the right side refers to the variable available only within that method (the one in the arguments).

Last edited:
Mark44
Mentor
The this keyword is provided in Java as a convenience to programmers so that they always have a handy reference to whatever nonstatic class instance happens to be in scope, without having to explicitly specify that class instance. C++ has the same keyword but refers to it as the this pointer, which holds the address of a nonstatic instance of a class, struct, or union.

In C++, several overloaded operators need you to return the "this" pointer, which is not found in Java. Because native Java (implemented in C/C++) already handles all of this underneath.