Scobal Scobal - 1 year ago 104
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download