Is it OK to use Gson instance as a static field in a model bean (reuse)?

前端 未结 4 1188
逝去的感伤
逝去的感伤 2020-11-29 02:44

Here\'s the model I implemented:

public class LoginSession {
    private static final Gson gson = new Gson();

    private String id;
    private String name         


        
4条回答
  •  死守一世寂寞
    2020-11-29 03:07

    It seems just fine to me. There is nothing in the GSON instance that makes it related to a specific instance of LoginSession, so it should be static.

    GSON instances should be thread-safe, and there was a bug regarding that which was fixed.

提交回复
热议问题