Scobal Scobal - 16 days ago 5
Java Question

Annotate anonymous inner class

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() {}
});
}

dty dty
Answer

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.