When should I use “this” in a class?

后端 未结 18 2189
情书的邮戳
情书的邮戳 2020-11-21 23:36

I know that this refers to a current object. But I do not know when I really need to use it. For example, will be there any difference if I use x i

相关标签:
18条回答
  • 2020-11-22 00:04

    Will be there any difference if I use "x" instead of "this.x" in some of the methods?

    Usually not. But it makes a difference sometimes:

      class A {
         private int i;
         public A(int i) {
            this.i = i; // this.i can be used to disambiguate the i being referred to
         }
      }
    

    If I just use "method()", will it not be, by default, applied to the current object?

    Yes. But if needed, this.method() clarifies that the call is made by this object.

    0 讨论(0)
  • 2020-11-22 00:05

    when there are two variables one instance variable and other local variable of the same name then we use this. to refer current executing object to avoid the conflict between the names.

    0 讨论(0)
  • 2020-11-22 00:06

    The only need to use the this. qualifier is when another variable within the current scope shares the same name and you want to refer to the instance member (like William describes). Apart from that, there's no difference in behavior between x and this.x.

    0 讨论(0)
  • 2020-11-22 00:06

    Google turned up a page on the Sun site that discusses this a bit.

    You're right about the variable; this can indeed be used to differentiate a method variable from a class field.

        private int x;
        public void setX(int x) {
            this.x=x;
        }
    

    However, I really hate that convention. Giving two different variables literally identical names is a recipe for bugs. I much prefer something along the lines of:

        private int x;
        public void setX(int newX) {
            x=newX;
        }
    

    Same results, but with no chance of a bug where you accidentally refer to x when you really meant to be referring to x instead.

    As to using it with a method, you're right about the effects; you'll get the same results with or without it. Can you use it? Sure. Should you use it? Up to you, but given that I personally think it's pointless verbosity that doesn't add any clarity (unless the code is crammed full of static import statements), I'm not inclined to use it myself.

    0 讨论(0)
  • 2020-11-22 00:06

    Following are the ways to use ‘this’ keyword in java :

    1. Using this keyword to refer current class instance variables
    2. Using this() to invoke current class constructor
    3. Using this keyword to return the current class instance
    4. Using this keyword as method parameter

    https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

    0 讨论(0)
  • 2020-11-22 00:07

    There are a lot of good answers, but there is another very minor reason to put this everywhere. If you have tried opening your source codes from a normal text editor (e.g. notepad etc), using this will make it a whole lot clearer to read.

    Imagine this:

    public class Hello {
        private String foo;
    
        // Some 10k lines of codes
    
        private String getStringFromSomewhere() {
            // ....
        }
    
        // More codes
    
        public class World {
            private String bar;
    
            // Another 10k lines of codes
    
            public void doSomething() {
                // More codes
                foo = "FOO";
                // More codes
                String s = getStringFromSomewhere();
                // More codes
                bar = s;
            }
        }
    }
    

    This is very clear to read with any modern IDE, but this will be a total nightmare to read with a regular text editor.

    You will struggle to find out where foo resides, until you use the editor's "find" function. Then you will scream at getStringFromSomewhere() for the same reason. Lastly, after you have forgotten what s is, that bar = s is going to give you the final blow.

    Compare it to this:

    public void doSomething() {
        // More codes
        Hello.this.foo = "FOO";
        // More codes
        String s = Hello.this.getStringFromSomewhere();
        // More codes
        this.bar = s;
    }
    
    1. You know foo is a variable declared in outer class Hello.
    2. You know getStringFromSomewhere() is a method declared in outer class as well.
    3. You know that bar belongs to World class, and s is a local variable declared in that method.

    Of course, whenever you design something, you create rules. So while designing your API or project, if your rules include "if someone opens all these source codes with a notepad, he or she should shoot him/herself in the head," then you are totally fine not to do this.

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