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

  • Context: Java 
  • Thread starter Thread starter icecubebeast
  • Start date Start date
  • Tags Tags
    Java Works
Click For Summary

Discussion Overview

The discussion centers around the use and understanding of the ".this" keyword in Java, particularly in the context of classes and objects. Participants explore its purpose, applications, and implications in coding practices.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the purpose and usage of ".this" in Java, noting that their learning resource does not adequately explain it.
  • Another participant describes ".this" as a reference to the current class and its parent classes, providing a code example to illustrate its use.
  • It is mentioned that ".this" acts as a "self" reference, particularly useful for distinguishing between class members and method parameters with the same name.
  • Some participants suggest that while using ".this" is not the only way to manage variable naming, it can simplify code readability in larger classes.
  • Concerns are raised about the potential for confusion when using multiple variables of the same name, with ".this" helping to clarify which variable is being referenced.
  • A participant notes that the ".this" keyword is a convenience for programmers, allowing easy reference to non-static class instances without needing to specify the instance explicitly.
  • Comparisons are made to C++, where a similar concept exists as the "this" pointer, with some differences in usage and requirements.

Areas of Agreement / Disagreement

Participants generally agree on the utility of ".this" for clarifying variable references, especially when names overlap. However, there is no consensus on whether it is the only or best approach to managing variable names in complex classes, as some argue for the feasibility of using different names instead.

Contextual Notes

Some participants highlight the limitations of their explanations, indicating that understanding of ".this" may depend on familiarity with object-oriented programming concepts and the specific context of its use.

icecubebeast
Messages
66
Reaction score
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
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;
}
 
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?
 
"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?
 
  • Like
Likes   Reactions: Routaran
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   Reactions: Medicol
How will you know which variable is which when you use it more than once?
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 39 ·
2
Replies
39
Views
8K
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K