What is the meaning of “this” in Java?

后端 未结 21 2565
走了就别回头了
走了就别回头了 2020-11-21 05:41

Normally, I use this in constructors only.

I understand that it is used to identify the parameter variable (by using this.something), if i

相关标签:
21条回答
  • 2020-11-21 06:13

    (I know Im late but shh Im being the sneaky boi you never saw me)

    The this keyword in most Object Oriented programming languages if not all means that its a reference towards the current object instance of that class. It's essentially the same thing as calling on that object from outside of the method by name. That probably made no sense so Ill elaborate:

    Outside of the class, in order to call something within that instance of the object, for example a say you have an object called object and you want to get a field you would need to use

    object.field
    

    Say for instance you are trying to access object.field from inside of your class in say, your constructor for example, you could use

    this.field
    

    The this keyword essentially replaces the object name keyword when being called inside of the class. There usually isn't much of a reason to do this outside of if you have two variables of the same name one of which being a field of the class and the other just being a variable inside of a method, it helps decipher between the two. For example if you have this: (Hah, get it? this? Hehe .... just me? okay :( I'll leave now)

    public String Name;
    //Constructor for {object} class
    public object(String Name){
        Name = Name;
    }
    

    That would cause some problems, the compiler wouldn't be able to know the difference between the Name variable defined in the parameters for the constructor and the Name variable inside of your class' field declarations so it would instead assign the Name parameter to.... the value of the Name parameter which does nothing beneficial and literally has no purpose. This is a common issue that most newer programs do and I was a victim of as well. Anyways, the correct way to define this parameter would be to use:

    public String Name;
    //Constructor for {object} class
    public object(String Name){
        this.Name = Name;
    }
    

    This way, the compiler knows the Name variable you are trying to assign is a part of the class and not a part of the method and assigns it correctly, meaning it assigns the Name field to whatever you put into the constructor.

    To sum it up, it essentially references a field of the object instance of the class you are working on, hence it being the keyword "this", meaning its this object, or this instance. Its a good practice to use this when calling a field of your class rather than just using the name to avoid possible bugs that are difficult to find as the compiler runs right over them.

    0 讨论(0)
  • 2020-11-21 06:14

    I was also looking for the same answer, and somehow couldn't understand the concept clearly. But finally I understood it from this link

    this is a keyword in Java. Which can be used inside method or constructor of class. It(this) works as a reference to a current object whose method or constructor is being invoked. this keyword can be used to refer any member of current object from within an instance method or a constructor.

    Check the examples in the link for a clear understanding

    0 讨论(0)
  • 2020-11-21 06:15

    this can be used inside some method or constructor.

    It returns the reference to the current object.

    0 讨论(0)
  • 2020-11-21 06:17

    In Java "this" is a predefined variable. If we use "this" in method that's mean we are getting the reference (address) of the currently running object. For an example.

    this.age ---> age of the currently running object.

    0 讨论(0)
  • 2020-11-21 06:18

    It's "a reference to the object in the current context" effectively. For example, to print out "this object" you might write:

    System.out.println(this);
    

    Note that your usage of "global variable" is somewhat off... if you're using this.variableName then by definition it's not a global variable - it's a variable specific to this particular instance.

    0 讨论(0)
  • 2020-11-21 06:19

    Objects have methods and attributes(variables) which are derived from classes, in order to specify which methods and variables belong to a particular object the this reserved word is used. in the case of instance variables, it is important to understand the difference between implicit and explicit parameters. Take a look at the fillTank call for the audi object.

    Car audi= new Car();
    
    audi.fillTank(5); // 5 is the explicit parameter and the car object is the implicit parameter 
    

    The value in the parenthesis is the implicit parameter and the object itself is the explicit parameter, methods that don't have explicit parameters, use implicit parameters, the fillTank method has both an explicit and an implicit parameter.

    Lets take a closer look at the fillTank method in the Car class

    public class Car()
    {
       private double tank;
    
       public Car()
       {
          tank = 0;
       }
    
       public void fillTank(double gallons)
       {
          tank = tank + gallons;
       }
    
    }
    

    In this class we have an instance variable "tank". There could be many objects that use the tank instance variable, in order to specify that the instance variable "tank" is used for a particular object, in our case the "audi" object we constructed earlier, we use the this reserved keyword. for instance variables the use of 'this' in a method indicates that the instance variable, in our case "tank", is instance variable of the implicit parameter.

    The java compiler automatically adds the this reserved word so you don't have to add it, it's a matter of preference. You can not use this without a dot(.) because those are the rules of java ( the syntax).

    In summary.

    • Objects are defined by classes and have methods and variables
    • The use of this on an instance variable in a method indicates that, the instance variable belongs to the implicit parameter, or that it is an instance variable of the implicit parameter.
    • The implicit parameter is the object the method is called from in this case "audi".
    • The java compiler automatically adds the this reserved word, adding it is a matter of preference
    • this cannot be used without a dot(.) this is syntactically invalid
    • this can also be used to distinguish between local variables and global variables that have the same name
    • the this reserve word also applies to methods, to indicate a method belongs to a particular object.
    0 讨论(0)
提交回复
热议问题