The use of “this” in Java

后端 未结 13 1853
-上瘾入骨i
-上瘾入骨i 2020-12-09 21:58

If I write the following class:

public class Example {

      int j;
      int k;

      public Example(int j, int k) {
           j = j;
           k = k;
          


        
相关标签:
13条回答
  • 2020-12-09 22:49

    Another useful approach (though seldom used) is to declare method parameters final:

    The following block will not compile, thus alerting you to the error immediately:

      public Example(final int j, final int k) {
           j = j;
           k = k;
      }
    
    0 讨论(0)
  • 2020-12-09 22:51

    To answer your follow-up question about this with method, the inner class mentioned by Srikanth is still a valid example of using this: (with method this time)

    public class OuterClass
    {
        void f() {System.out.println("Outer f()");};
        class InnerClass {
            void f() {
                System.out.println("Inner f()");
                OuterClass.this.f();
            }
        }
    }
    

    You have the same situation with anonymous class:

    You can refer to the outer class’s methods by:

    • MyOuterClass.this.yOuterInstanceMethod(),
    • MyOuterClass.myOuterInstanceMethod(),
    • or simply myOuterInstanceMethod() if there is no ambiguity.
    0 讨论(0)
  • 2020-12-09 22:52

    What you are experiencing is called variable shadowing. Have a look at this overview for different kinds of variables in Java.

    Generally speaking: The Java compiler uses the nearest variable it can find for an assignment. In a method it will first try to find a local variable and then enlarge the focus of its search to class and instance variables.

    One habit I personally find good (others don't like it) is prefixing a member variable with m_ and using uppercase for CONSTANT_VARIABLES that don't change their value. Code where variable shadowing is used on purpose is very(!) difficult to debug and work with.

    0 讨论(0)
  • 2020-12-09 22:55

    If you say just j in your constructor then the compiler will think you mean the argument in both cases. So

    j = j;
    

    simply assigns the value of the argument j to the argument j (which is a pretty pointless, but nonetheless valid statement).

    So to disambiguate this you can prefix this. to make clear that you mean the member variable with the same name.

    The other use of this is when you need to pass a reference to the current object to some method, such as this:

    someObject.addEventListener(this);
    

    In this example you need to refer to the current object as a whole (instead of just a member of the object).

    0 讨论(0)
  • 2020-12-09 22:58

    If you don't write "this.variable" in your constructor, and if you have a local variable (including the function parameter) with the same name as your field variable in the constructor, then the local variable will be considered; the local variable shadows the field (aka class variable).

    One place where "this" is the only way to go:

    class OuterClass {
      int field;
    
      class InnerClass {
        int field;
    
        void modifyOuterClassField()
        {
          this.field = 10; // Modifies the field variable of "InnerClass"
          OuterClass.this.field = 20; // Modifies the field variable of "OuterClass",
                                      // and this weird syntax is the only way.
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-09 23:00

    If you don't write this, then you assign the argument to itself; the argument variables shadow the instance variables.

    0 讨论(0)
提交回复
热议问题