Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Dec 22, 2014 #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.
     
  2. jcsd
  3. Dec 22, 2014 #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;
    }
     
     
  4. Dec 22, 2014 #3
    Can you explain what it does and what the applications are?
     
  5. Dec 22, 2014 #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.
     
  6. Dec 23, 2014 #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. Dec 26, 2014 #6

    Borg

    User Avatar
    Gold Member

  8. Dec 27, 2014 #7
    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.
    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.
     
  9. Dec 27, 2014 #8
    How will you know which variable is which when you use it more than once?
     
  10. Dec 27, 2014 #9
    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:
    Code (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: Dec 27, 2014
  11. Jan 1, 2015 #10

    Mark44

    Staff: 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.
     
  12. Jan 1, 2015 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook