简体   繁体   中英

Using an array of ElementType gives error in Target annotation

import java.lang.annotation.ElementType.*;
import java.lang.annotation.Target;

@Target({TYPE, FIELD, METHOD})
public @interface Inter1 {
}

I've seen in other annotations like SuppressWarnings and Deprecated that they use an array of ElementType enum directly without using the enum like the above. But when I try to use it in one of my custom annotations, it gives me an error. I need to give it as ElementType.TYPE or ElementType.FIELD for it to work. What is wrong with this code?

you want to use that annotation this way:

import static java.lang.annotation.ElementType.*;

otherwise, this way:

@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM