Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to init Gson object

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);
}
like image 682
ua741 Avatar asked May 06 '15 13:05

ua741


1 Answers

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();
like image 139
MrEngineer13 Avatar answered Oct 15 '22 21:10

MrEngineer13