Search code examples
javajsongsonpojo

Java - is it possible to declare a serialized key by the name "static"


I wanted to create a POJO which I will later want to convert it to JSON using GSON. My JSON looks like this

{
"static":"value",
"otherkey": "value"
}

So my POJO looks likes this

public class MyPOJO {

    public String static;
    public String otherkey;
}

But the complier complains me at this public String static;. I know static is a keyword but is it possible?


Solution

  • No, you can't name it static because it's a reserved keyword, just like the other answers say. To solve your problem with GSON, use the @SerializedName annotation to specify the name used during serialization, and don't name the variable static, pick something else like so:

    @SerializedName("static")
    private String myStaticVar;