-1

Let's say we have a class

public class ThisTest {
    String username;

    ThisTest(String s) {
        this.username = s;
    }

    public void sayHello() {
        System.out.println("Hello " + this.username);
    }

    public static void main(String args[]) {
        ThisTest a = new ThisTest("Jack");

        ThisTest b = new ThisTest("George");

        a.sayHello();
        b.sayHello();
    }
}

Output (as expected):

Hello Jack 

Hello George

My question is: inside the sayHello method, how does the runtime determine which object instance on the heap "this" refers to.

on compilation, is the method call passed a hidden reference to the calling object (a or b) which is then accessed using the "this" keyword ? - This is what C++ does.

or, is "this" a hidden instance variable of the object on the heap created when the object is constructed?

or, is there some other mechanism by which the sayHello method knows which object "this" refers to?

Essentially, how is "this" implemented at the compiler level in Java.

SR Bhaskar
  • 898
  • 7
  • 17
  • Did you try: [https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html) – Coder Oct 07 '17 at 19:43
  • 3
    "On compilation, is the method call passed a hidden reference to the calling object (a or b) which is then accessed using the "this" keyword ?" that is answer. There is hidden first argument for each method. You can also explicitly declare method as `yourMethod(YourClass this, [rest of parameters]) and call it via `foo.yourMethod(rest or arguments)` and `this` will hold same reference as `foo` (at time of calling that method). – Pshemo Oct 07 '17 at 19:45
  • The thiskey javadoc doesn't answer my question, John D – SR Bhaskar Oct 07 '17 at 19:45
  • How it is implemented at what level? Remember, there's (typically) a JVM involved in which objects are first class. – pvg Oct 07 '17 at 19:46
  • How is it implemented at the compiler level. Let me update my question. – SR Bhaskar Oct 07 '17 at 19:47
  • Thanks Pshemo, that answers my question. – SR Bhaskar Oct 07 '17 at 19:53
  • 1
    Not necessary duplicate, but related: [Meaning of “this” in this code?](https://stackoverflow.com/q/44095724) – Pshemo Oct 07 '17 at 19:57
  • @programmerravi you could always try decompiling the code to see what the compiler does. It's actually quite clear if you compare the instructions for a static Vs non-static method with the same parameters/return type. (Using `javap -c YourClass`) – Andy Turner Oct 07 '17 at 20:01
  • Okay thanks, ill take a look at the instructions generated by the compiler. – SR Bhaskar Oct 07 '17 at 20:11

1 Answers1

1

From a Java programming language point, there is no mandated implementation, as even compiling to Java byte code is considered “normal”, but not required.

Java Language Specification, Chapter 1. Introduction

The Java programming language is normally compiled to the bytecode instruction set and binary format defined in The Java Virtual Machine Specification, Java SE 8 Edition.

To find out, how the this reference is implemented when compiling for the Java Virtual Machine, we have to refer to the mentioned The Java Virtual Machine Specification:

2.6.1. Local Variables

Each frame (§2.6) contains an array of variables known as its local variables. The length of the local variable array of a frame is determined at compile-time and supplied in the binary representation of a class or interface along with the code for the method associated with the frame (§4.7.3).

Local variables are addressed by indexing. The index of the first local variable is zero. […]

The Java Virtual Machine uses local variables to pass parameters on method invocation. On class method invocation, any parameters are passed in consecutive local variables starting from local variable 0. On instance method invocation, local variable 0 is always used to pass a reference to the object on which the instance method is being invoked (this in the Java programming language). Any parameters are subsequently passed in consecutive local variables starting from local variable 1.

In other words, in Java byte code, this is compiled like a local variable (with index zero) that is pre-initialized with the object reference when entering the method. It can be accessed with the same byte code instructions that are used to access the parameter variables or explicitly declared local variables.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765