Is there a way to annotate an anonymous inner class in Java?
In this example could you add a class level annotation to Class2?
public void method1() {
add(new Class2() {
public void method3() {}
});
}
Is there a way to annotate an anonymous inner class in Java?
In this example could you add a class level annotation to Class2?
public void method1() {
add(new Class2() {
public void method3() {}
});
}
No. You'd need to promote it to a "proper" class. It can still be scoped within the outer class if necessary, so it doesn't need to be a top-level class, or public, or whatever. But it does need a proper class definition to attach the annotation to.