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

  • #1
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.
 

Answers and Replies

  • #2
156
203
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;
}
 
  • #3
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?
 
  • #4
156
203
"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.
 
  • #5
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?
 
  • #7
259
786
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.
 
  • Like
Likes Medicol
  • #8
How will you know which variable is which when you use it more than once?
 
  • #9
259
786
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:
  • #10
34,667
6,379
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.
 
  • #11
223
54
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.
 

Related Threads on (Java) I don't get how ".this" works?

Replies
7
Views
149K
Replies
4
Views
9K
Replies
19
Views
913
Replies
25
Views
2K
Replies
8
Views
254
Replies
2
Views
3K
Replies
34
Views
770
Replies
3
Views
2K
Replies
20
Views
2K
Top