Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization of static inner class

Can we serialize a static inner class in Java? The class itself is declared as static here.

like image 512
Tinkerbel Avatar asked Apr 09 '12 13:04

Tinkerbel


People also ask

Can static class be serialized?

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI). But, static variables belong to class therefore, you cannot serialize static variables in Java.

Can inner class be serializable?

Because inner classes cannot declare static members other than compile-time constant fields, they cannot use the serialPersistentFields mechanism to designate serializable fields.

Can you serialize a static class C#?

There are never any instances of static classes: they are both abstract and sealed in the IL, so the CLR will prevent any instances being created. Therefore there is nothing to serialize. Static fields are never serialized, and that's the only sort of state that a static class can have.

Are static methods serialized?

Static variables are not serialized. Serializability has nothing to do with it.


1 Answers

@Tinkerbel something to note -

Serializing static nested classes is fine but when (in future) if static inner classes are ever moved to a different outer class then de serializing the old objects is a problem.

All the fields are assigned to be default/null.

like image 181
user2674407 Avatar answered Sep 28 '22 07:09

user2674407