简体   繁体   English

Java注释将字段设置为其名称

[英]Java annotation to set a field to it's name

Is there any Java annotation that sets a field value to it's name, for example: 是否有任何Java注释将字段值设置为其名称,例如:

public interface Protocol {
    @Whatever String START; // Here @Whatever annotation would set START to "START" in any static string field.
}

No, But you can use enum class. 不,但是您可以使用枚举类。

public enum Protocol  {
   START,
   END
}

And then when you will write: 然后当你写的时候:

System.out.println(Protocol.START);

You will get "START" 您将获得“开始”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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