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

  • Java
  • Thread starter icecubebeast
  • Start date
  • Tags
    Java Works
In summary, this keyword is a convenience provided in Java that refers to whatever nonstatic class instance is in scope.
  • #1
icecubebeast
66
3
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.
 
Technology news on Phys.org
  • #2
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
Silicon Waffle said:
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
"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
icecubebeast said:
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.
icecubebeast said:
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
icecubebeast said:
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
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
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.
 

1. What is the purpose of "this" in Java?

The keyword "this" in Java refers to the current object instance. It can be used to access variables and methods within the current class. It is often used to differentiate between instance variables and local variables with the same name.

2. How does "this" differ from "super" in Java?

While "this" refers to the current object instance, "super" refers to the parent class of the current class. It is used to access variables and methods from the parent class. "this" and "super" are often used together to access and modify variables and methods from both the current and parent class.

3. Why is "this" sometimes used in method chaining?

In method chaining, multiple methods are called on the same object instance in a single line of code. "this" is used to refer to the current object instance within these methods, allowing them to manipulate the same object instead of creating a new one each time.

4. Can "this" be used in a static method?

No, "this" cannot be used in a static method as it refers to the current object instance and static methods do not have an associated object instance. Instead, the keyword "this" can be replaced with the class name to access static variables and methods.

5. How does "this" work with constructors in Java?

In a constructor, "this" can be used to call another constructor within the same class. This is useful when a class has multiple constructors with different parameters and one constructor needs to call another to avoid duplicate code. "this" can also be used to pass the current object instance as an argument to another method or constructor.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
969
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
5K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top