10

enter image description here

I am trying to understand this Functional Reactive Java library by running a test called testSendStream in debug mode and stepping through the code as the test executes.

The snapshot above shows that there is an oddly named variable called this$0.

Where does this name come from ?

What does this name mean ?

Why does this variable has this name ?

What is the reasoning behind giving it this name?

Surely this name does not come from the code itself, it is generated by IntelliJ or javac/java. But why ?

It is also interesting to see what happens if I mark this object by the label Mystery Object.

enter image description here

jhegedus
  • 20,244
  • 16
  • 99
  • 167

2 Answers2

17

this$0 is "hidden field" in Inner class (the non-static nested class) which is used to hold reference to instance of Outer class which was used to create current instance of Inner class.

In short when you have

Outer outer = new Outer();
Outer.Inner inner = outer.new Outer.Inner(); 

Inner instance held by inner will store in its this$0 field reference to Outer instance used to create it (same reference as held by outer variable).

It is necessary because nested classes must have access to all members of outer classes (including private ones). If we want to be able to write something like methodFromOuterClass(); in inner class JVM needs to know on which Outer instance it should invoke this method. To make it possible compiler "changes" such code to this$0.methodFromOuterClass().


Little more details and example:

public class Outer {
    private int id;
    public Outer(int id) { this.id = id;}

    public class Inner{
        void printOuterID(){
            System.out.println(id); 
        }
    }
}

Now what will be printed here and why?

Outer o1 = new Outer(1);
Outer o2 = new Outer(2);
Outer.Inner in1 = o1.new Inner();
Outer.Inner in2 = o2.new Inner();

in1.printOuterID();
in2.printOuterID();

We will see

1
2

but how in1 knew that it should print value of id from o1 and not from o2?
It is because each instance of inner class knows on which instance of outer class was it created. And that is because of this$0 reference which stores reference to outer instance used to create inner instance.
This variable is added to all non-static inner classes by compiler and its value is set when you invoke

Outer.Inner in1 = o1.new Inner(); //`this$0` will be set to hold `o1` instance.

So code like

void printOuterID(){
    System.out.println(id); 
  //OR
  //System.out.println(Outer.this.id);
}

is compiled into

void printOuterID(){
    System.out.println(this$0.id); //although we can't access this$0 explicitly
}

BTW if your inner class doesn't need to access non-static members of any of its outer classes you may change it to static class which will get rid of this$0 field.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Maybe worth to mention that an inner class does not need an `Outer.this` and one could make the inner class a _static_ embedded class. Sometimes `static` is just forgotten. – Joop Eggen Sep 26 '22 at 11:09
  • @JoopEggen Done - assuming I understood you correctly. If I didn't feel free to improve it. – Pshemo Sep 26 '22 at 12:20
2

The is a convention related to non static inner classes. The bytecode of the inner class will contain a reference to a package-scoped field named this$0 that allows you to refer to the this object of the enclosing class. Notice in your example this$0 is the same as the Mystery Object this variable defined above it.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124