You can both access it through the JDI and through reflection. The enclosing instance is stored as a field of instances of the inner class Lasanga
. The automatically-generated name for the field is usually this$0
(so in the example above, the field with this name has type Garfield
).
To access it in the JDI, you have to use the ReferenceType of the ObjectReference. There are three relevant methods of ReferenceType:
- fields() gives you all simple fields and also these synthetic fields
- visibleFields() additionally gives you inherited fields
- allFields() additionally gives you hidden fields (and possibly repeats synthetic fields)
Accessing it through reflection is the same as usual, just ask for the field of name "this$0"
.
But, you can't access the synthetically-defined variable at compile time, asking for the field this$0
will cause a compile-time error.