简体   繁体   中英

Implementing Marker Interfaces in Java

I want to implement a marker interface that suggests user to define a value like serialVersionUID that is suggested when a Class implements java.io.Serializable . How can I achieve it in Java?

Thanks in advance!!

If you can go the annotation way you can implement a custom annotation with a (required) element. Elements that do not have a default value are required .

For instance look at the following annotation:

public @interface Deprecated {
    String version(); //In which version this was deprecated?
    String reason() default "[not documented]";
}

Here version is a required field, while you can omit the reason field when annotating something.

For more information, you can look at this tutorial: http://howtodoinjava.com/2014/06/09/complete-java-annotations-tutorial/

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