Why static fields not serialized using google.gson.GsonBuilder JSON parser?

11,420

Java Serialization only serialize object's non-static and non-transient fields because,

The defaultReadObject method uses information in the stream to assign the fields of the object saved in the stream with the correspondingly named fields in the current object. This handles the case when the class has evolved to add new fields. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

Reference

In case of static field state not only belongs to any specific object it will belongs to all class.

So the static field would be comes under state of any specific object.

Share:
11,420
Saro Taşciyan
Author by

Saro Taşciyan

I'm a software developer started as a self taught amateur programmer when I was fourteen years old. At university I worked on various game projects. Beginning in third year I focused on .NET technologies. After graduation I started working on ASP.NET Web Applications and Win CE as .NET Developer. Although I want to expertise in .NET technologies, I have been developing Android applications using Java for nearly two years and I'm always interested in learning new technologies.

Updated on June 09, 2022

Comments

  • Saro Taşciyan
    Saro Taşciyan almost 2 years

    I tried serializing an object using google.gson.GsonBuilder as follows:

    public class JsonHelper
    {
        public static String ToJson(Object o, Type oType)
        {
            Gson gson = new().setPrettyPrinting().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
            gson.toJson(o, oType);
        }
    }
    
    public class JsonTest
    {
       public static String staticField;
    
       public static String ToJson()
       {
           JsonTest newJsonTest = new JsonTest();
           newJsonTest.staticField = TelephoneStatus.GetPhoneIMEI(); // let's say we use static field to keep IMEI
    
           Type oType = new TypeToken<JsonTest>(){}.getType();
           return JsonHelper.ToJson(newJsonTest, oType);
       }
    }
    

    Return value for JsonTest class method ToJson() is empty. If i change staticField field declaration to be non-static, it works as expected. Considering why static fields are not serialized, should it be considered as a bug? Or is it considered unnecessary?

    If i had a list of JsonTest i wouldn't expect having static field parsed and written multiple times but once. However, isn't it better than missing it at all?

  • Subhrajyoti Majumder
    Subhrajyoti Majumder over 11 years
    The whole serialization concept is same as well and that is applicable to all. :)
  • user207421
    user207421 over 11 years
    So you assert. You can't prove that by quoting one of the other systems. You need to quote the relevant documentation.