Pergunta

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.

Foi útil?

Solução

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

Outras dicas

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;
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top