If I have
[EDIT: added the type definition for “Inner”]
interface Inner{
public void execute();
}
class Outer{
int outerInt;
public void hello(){
Inner inner = new Inner(){
public void execute(){
outerInt=5;
}
}
//later
inner.execute();
}
}
will the call to inner.execute() set the outerInt variable of that particular Outer object to 5, wherever it is called from, and for as long as that Inner object exists? Or will it just change a copy of the outerInt variable and not affect the original Outer object?
To answer your newly clarified question (from the comment to my other answer):
Yes.
All inner and local classes will have a reference to their parent’s
this, even if they never use it.Demo.