Out of the following two option, which one do you prefer to init Gson object? Can someone help me understand the impact of both ways on Android app performance (start up time?), if there's any.
// inline creation
private final Gson gson = new Gson();
vs
// need base creation, overhead of sync everytime while accessing gson object
private synchronized Gson getOrCreateGson() {
gson == null ? gson = new Gson() : gson;
return gson.fromJson(jsonString, clazz);
}
It's up to you but there's nothing wrong with a private static final
to take care of this. You can read more about it at this related question.
private static final Gson gson = new Gson();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With