Java – serialize an object with a non serializable parent class

How does the following code work?

class A {
         int a = 10;
     }


     class B extends A implements Serializable{

      }



     public class Test {
       public static void main(String[] args){
        B obj = new B();
        obj.a = 25;


        //Code to serialize object B  (B b= new B()),// deserialize it and print the value of 'a'. 
      }
    }

Even if I change the value of 'a' in the code, the code will print 10

Any explanation for this behavior?

Solution

The default value for a is 10 – it is set to 10 when creating objects If you want to do an actual test, set it to a different value after instantiation, and then serialize it

As for your update - if a class is not serializable, its fields will not be serialized and deserialized Only serialized subclass fields

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>