Java, a way to use reserved words

本秂侑毒 提交于 2019-12-24 08:58:31

问题


Is there a method that allows you to use a keyword as a resource name in Java similar to the method used in C# of adding @ to the start of the word?

I found some posts asking this a few years ago and it wasn't possible then. Was it possibly added though in Java 7 or may be added in Java 8?

Reasons for doing this are very scarce. For me though I've encountered it when deserializing large json objects that use the keywords as variable names. A particular json object I'm working with uses throw. In C# I'd just add "string @throw" into my class that I use to deserialize the object. The only methods I can find to do this in java would require a streaming deserialization which would be a major pain, or string parsing with Regular Expressions both of which seem to be a lot of work for what could otherwise be a super simple task.


回答1:


No, this is not possible in Java. The names of variables
in Java cannot coincide with any keywords or reserved words.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html




回答2:


It's possible by using GSON's Field Naming Support

eg.

 @SerializedName("new")
    private String New;
   public String getNew ()
    {
        return New;
    }
    public void setNew (String aNew)
    {
        New = aNew;
    }


来源:https://stackoverflow.com/questions/20814287/java-a-way-to-use-reserved-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!