Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:31:55+00:00 2026-06-18T02:31:55+00:00

I tried serializing an object using google.gson.GsonBuilder as follows: public class JsonHelper { public

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:31:56+00:00Added an answer on June 18, 2026 at 2:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem serializing an object using Gson. @XmlRootElement class Foo implements Serializable
I've tried using the standard serializing type things, stuff like: FileOutputStream f_out; try {
I want to deserialize a JSON object (using GSON, because I already use it
I am getting the following error message when trying to serialize List<Tuple<string, Type, object>>
I am serializing an object like this: XmlSerializer serializer = new XmlSerializer(obj.GetType()); using (StringWriter
I have a common code of serializing a class object in my 3-4 methods
Tried to create a mobile navigation using dataview or outline and the view renders
Tried following the instructions here: How to use Google app engine with my own
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
Using ActiveObjects as my ORM and Gson as my JSON processor. Ran into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.