Vinayak Bevinakatti Vinayak Bevinakatti -4 years ago 34
Java Question

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

"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?

Answer Source

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:

private String myStaticVar;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download