简体   繁体   中英

'this' in Java: How does it work?

I know that 'this' is acting as reference in Java. We can use it inside the class members only.

What I am asking is... because it is used in the members of the class, that means it has to be either an instance variable or parameter.

And assume, if it is param to a method but it is working in blocks. block does not contain any params and all ..could you explain what is it ...how exactly it was defined in java?How exactly it is using By JVM.

From a linguistic point of view, this is neither a local variable or a parameter. Syntactically, it is a keyword. Semantically, it is an explicit way of saying "the current object"; see JLS 15.8.3 . For example:

  • this.<attributeName> explicitly refers to an instance level attribute of the current object.
  • <methodName>(this) calls a method, passing a reference to the current object as an explicit argument.

The this keyword has other uses in Java that don't exactly mean "the current object":

  • this(<optArgumentList>) as the first statement in a constructor chains to another constructor in the same class; JLS 8.8.7 .
  • <className>.this within an inner class refers to the instance of an enclosing class for the current object; JLS 15.8.4 .

From an implementation perspective, you can think of the "this" reference as a hidden or implicit parameter that gets passed each time an instance method is called. Indeed, this is more or less how the object reference is treated by the JVM's "invoke*" bytecodes. You push the target object reference onto the "opstack" followed by each of the argument values, then execute the "invoke..." instruction. (Look here for the details.).

Instance methods are invoked within the context of an actual instance, an actual object.

Within the instance method of a class , this (in most context) refers to this object. This is a relative reference: when the same method is invoked on another object, then this within the execution of that method now refers to this second object.

For completeness, it should be mentioned that Java also has what is called "qualified" this that can be used to refer to not the object that the method is invoked upon, but the enclosing object of an inner class .

It can also appear, with some restriction, in constructors, to invoke another constructor of the same class. super can also be used in this manner.

References

  • JLS 15.8.3 this
    • When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked, or to the object being constructed.
  • JLS 15.8.4 Qualified this
    • Any lexically enclosing instance can be referred to by explicitly qualifying the keyword this .
  • JLS 8.8.7.1 Explicit Constructor Invocations
    • Alternate constructor invocations begin with the keyword this (possibly prefaced with explicit type arguments). They are used to invoke an alternate constructor of the same class.

How exactly was [this] defined in Java?

In Java this is defined by the Java Language Specification as a keyword and therefore is not an ordinary variable name and has special behaviour.

The behaviour of this is defined in section 15.8.3 of the specification. For example:

When used as a primary expression, the keyword this denotes a value, that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed.

"this" is implicitly passed as argument for every non-static method call you make. You can think of it as syntactical sugar, but at machine level, it is indeed an additional parameter which gets passed.

class Person
{
  string name;
  int age;
  void print(){ System.out.writeln(name+" "+age); }
}

works like that (pseudo-code):

class Person
{
  string name;
  int age;
}

void print(Person this)
{
  System.out.writeln(this.name+" "+this.age);
}

One way of thinking about it is as an implicit parameter to instance methods and constructors. In the case of a constructor it's the job of the constructor to set up "this", while in the case of instance methods, they can operate on "this" as appropriate.

Note that Java also has some syntactic sugar allowing you to implicitly reference fields and methods of "this" without specifying this.foo .

this will give you a reference to the actual instance. It's very common in constructors, eg:

class Dog {
   String name;

   public Dog(String name) {
      this.name = name;
   }
}

You can think of 'this' refer to itself. It's like me.[attribute] to access itself attributes or methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM