Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the meaning of "this.this$0"?

Tags:

java

android

what's the meaning of "this.this$0" in this code? what does it stands for? I know why we use "this" but I have no idea about "this.this$0"

 class MainActivity$1 implements TextWatcher
{
  public void afterTextChanged(Editable paramEditable)
  {
  }

  public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
  {
  }

  public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3)
  {
    this.this$0.ChangeToNumber(paramCharSequence.toString());
  }
}
-----------------------or ----------------------
class MainActivity$2 implements View.OnClickListener
{
  public void onClick(View paramView)
  {
    this.this$0.startActivity(new Intent(this.this$0, about.class));
  }
}
like image 948
jack peterson Avatar asked Nov 26 '14 09:11

jack peterson


3 Answers

this.this$0 it's same to Main.access$0 These mysterious symbols usually correspond to the anonymous inner classes. The Java VM doesn't know about them, only about top-level classes, so the Java compiler provides several workarounds to make inner classes to work.

Local class has implicit reference to the instance of its enclosing class,'this$0' corresponds to this reference in the decompiled code. JVM prevents classes from accessing privates methods of other classes so the compiler generates several synthetic package-private methods like access$0 in order to access private methods of enclosing instance.

There are many others features of the Java language that are implemented with synthetic methods like generics and covariant return types.

I suggest you to check those links: Decoding Decompiled Source Code For Android

and : Performance Tips

like image 163
Ahmad MOUSSA Avatar answered Nov 16 '22 13:11

Ahmad MOUSSA


There's nothing preventing you (beside decent naming conventions) from having an instance member called this$0 and then referring to it with the this keyword.

For example :

public class SomeClass
{
    int this$0;
    public SomeClass (int val) 
    {
        this.this$0 = val;
    }
} 
like image 40
Eran Avatar answered Nov 16 '22 14:11

Eran


this$0 normally is for the parent object of a non-static inner class. E.g.,

public class Outer {
    class Inner1 {
        int f1 = 1;
    }

    static class Inner2 {
        int f1 = 2;
    }    

    public static void main(String[] args) {
        Outer o = new Outer();
        Outer.Inner1 i1 = o.new Inner1(); //weird but valid
        Outer.Inner2 i2 = new Outer.Inner2(); //normal
        //wrong: Outer.Inner1 i3 = new Outer.Inner1();
    }
}

Normally we define inner class as static. i2 has only 1 field, but i1 has an extra this$0 which points to o.

enter image description here

like image 4
Leon Avatar answered Nov 16 '22 13:11

Leon