Gson 为什么不将方法都写成静态方法?

我一直很困惑为啥谷歌不将Gson类中的方法改为静态方法,用起来不是更方便吗?
我的前同事有好多人在结合httpclient使用时都是在发送消息的类中定义了一个属性:
private static Gson gson = new Gson();
调用时,就直接
gson.toJson(obj);
这种使用方法有没有什么潜在问题?
求解答啊...

精慕HU
浏览 866回答 4
4回答

吃鸡游戏

多线程同时调用会有问题吧以下摘自Gson User Guide:Sometimes you need to share state across customserializers/deserializers (see this discussion). You can use thefollowing three strategies to accomplish this: 1.Store shared state in static fields2.Declare the serializer/deserializer as inner classes of a parent type, and use the instance fields of parent type to store shared state3.Use Java ThreadLocal 1 and 2 are not thread-safe options, but 3 is.

扬帆大鱼

弱弱的问一下,gson 是 json 写错了 还是有一种叫 gson 的语言?

蓝山帝景

我觉得可能是效率的问题,一旦写为 static ,则一加载就在内存中了,而这无疑会降低运行速度!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java