When should I use “this” in a class?

后端 未结 18 2188
情书的邮戳
情书的邮戳 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-21 23:51

    Unless you have overlapping variable names, its really just for clarity when you're reading the code.

    0 讨论(0)
  • 2020-11-21 23:54

    You only need to use this - and most people only use it - when there's an overlapping local variable with the same name. (Setter methods, for example.)

    Of course, another good reason to use this is that it causes intellisense to pop up in IDEs :)

    0 讨论(0)
  • 2020-11-21 23:54

    this is a reference to the current object. It is used in the constructor to distinguish between the local and the current class variable which have the same name. e.g.:

    public class circle {
        int x;
        circle(int x){
            this.x =x;
            //class variable =local variable 
        }
    } 
    

    this can also be use to call one constructor from another constructor. e.g.:

    public class circle {
        int x;
    
        circle() { 
            this(1);
        }
    
        circle(int x) {
            this.x = x; 
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:59

    To make sure that the current object's members are used. Cases where thread safety is a concern, some applications may change the wrong objects member values, for that reason this should be applied to the member so that the correct object member value is used.

    If your object is not concerned with thread safety then there is no reason to specify which object member's value is used.

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

    this does not affect resulting code - it is compilation time operator and the code generated with or without it will be the same. When you have to use it, depends on context. For example you have to use it, as you said, when you have local variable that shadows class variable and you want refer to class variable and not local one.

    edit: by "resulting code will be the same" I mean of course, when some variable in local scope doesn't hide the one belonging to class. Thus

    class POJO {
       protected int i;
    
       public void modify() {
          i = 9;
       }
    
       public void thisModify() {
          this.i = 9;
       }
    }
    

    resulting code of both methods will be the same. The difference will be if some method declares local variable with the same name

      public void m() {
          int i;
          i = 9;  // i refers to variable in method's scope
          this.i = 9; // i refers to class variable
      }
    
    0 讨论(0)
  • 2020-11-22 00:03

    this is useful in the builder pattern.

    public class User {
    
        private String firstName;
        private String surname;
    
        public User(Builder builder){
            firstName = builder.firstName;
            surname = builder.surname;
        }
    
        public String getFirstName(){
            return firstName;
        }
    
        public String getSurname(){
            return surname;
        }
    
        public static class Builder {
            private String firstName;
            private String surname;
    
            public Builder setFirstName(String firstName) {
                this.firstName = firstName;
                return this;
            }
    
            public Builder setSurname(String surname) {
                this.surname = surname;
                return this;
            }
    
            public User build(){
                return new User(this);
            }
    
        }
    
        public static void main(String[] args) {
            User.Builder builder = new User.Builder();
            User user = builder.setFirstName("John").setSurname("Doe").build();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题