Keyword for the outer class from an anonymous inner class
In general you use OuterClassName.this
to refer to the enclosing instance of the outer class.
In your example that would be a.this.otherMethod()
Access method of outer anonymous class from inner anonymous class
Since Java 8 the solution is pretty easy. Just store the method reference in a variable.
ReturnsANumber v = new ReturnsANumber() {
int theNumber() {
return 119;
}
public int getIt() {
Supplier<Integer> supplier = this::theNumber;
ReturnsANumber w = new ReturnsANumber() {
int theNumber() {
return 1;
}
public int getIt() {
return supplier.get();
}
};
return w.getIt();
}
};
Storing outer object could also do the trick. But for inherited methods only:
interface ReturnsANumber {
int theNumber();
int getIt();
}
public int getIt() {
ReturnsANumber outer = this;
ReturnsANumber w = new ReturnsANumber() {
public int theNumber() {
return 1;
}
public int getIt() {
return outer.theNumber();
}
};
return w.getIt();
}
You can store the method reference or the outer object as a field also.
Update
@Holger proposed another workaround. You can pass your outer object to a lambda:
ReturnsANumber v = new ReturnsANumber() {
...
@Override
public int getIt() {
ReturnsANumber w = Optional.of(this).map(outer ->
new ReturnsANumber() {
int theNumber() {
return 1;
}
public int getIt() {
return outer.theNumber();
}
}).get();
return w.getIt();
}
};
Does an anonymous inner class always capture a reference to this (outer) object when accessing its primitives etc.?
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.
Getting hold of the outer class object from the inner class object
Within the inner class itself, you can use OuterClass.this
. This expression, which allows to refer to any lexically enclosing instance, is described in the JLS as Qualified this
.
I don't think there's a way to get the instance from outside the code of the inner class though. Of course, you can always introduce your own property:
public OuterClass getOuter() {
return OuterClass.this;
}
EDIT: By experimentation, it looks like the field holding the reference to the outer class has package level access - at least with the JDK I'm using.
EDIT: The name used (this$0
) is actually valid in Java, although the JLS discourages its use:
The
$
character should be used only in
mechanically generated source code or,
rarely, to access pre-existing names on
legacy systems.
Get the reference to the class object of an anonymous inner class
If for reference to the anonymous you ask a reference to the anonymous class, the java.lang.Class
instance object to your anonymous class here is how you can do that.
If you assign the anonimous class instance to the variable obj
you can have a reference to the class with obj.getClass()
. The example uses Object
, but any non-final
class and any interface can be used.
Object obj = new Object() {
};
obj.getClass(); // Reference to the anonymous class
You can do the same also without explicitly creating a variable like obj for example
Button b = ...;
b.addActionListener(new ActionListener() {
....
});
ActionListener[] listeners = b.getActionListeners();
for (ActionListener listener : listeners) {
System.out.println(listener.getClass()); // Prints the reference to the class
}
If no reference to the an object of type 'Anonymous' can be used (at least with reflection) you can't do that.
How to change outer variable from anonymous inner class?
You're creating a Runnable class, but it actually never runs. You need to "start" it, by calling its start() method.
But you must also keep in mind, that when you start it inside the outerMethod(), it may not run before the Log method is called (since it will run in a separate thread) and the order in which the code is called is not guaranteed anymore.
How to refer to the containing class of an anonymous or nested class in Java
Yes. Like this:
public class OuterClass {
class InnerClass {
void method() {
// Refer to outer class instance
doSomething( OuterClass.this );
}
void doSomething(OuterClass outer) {
// ...
}
}
}
- See also: Access
this
from Java anonymous class
Related Topics
How Can a String Be Initialized Using " "
Javafx: Location Is Not Set Error
Any Way to Invoke a Private Method
Call "Java -Jar Myfile.Jar" with Additional Classpath Option
Initial Size for the Arraylist
Java Sslhandshakeexception "No Cipher Suites in Common"
Java:If a Extends B and B Extends Object, Is That Multiple Inheritance
How to Get Frequency from Fft Result
Java: How to Split an Arraylist in Multiple Small Arraylists
Rotating Coordinate Plane for Data and Text in Java
Call an Executable and Pass Parameters
How to Inject a Property Value into a Spring Bean Which Was Configured Using Annotations
How to Attach Source in Eclipse
What Is the Best Approach Using Jdbc for Parameterizing an in Clause