- Java Serializable Static Fields
- Java Serialization Static Fields In Word
- Serialization Java Example
- Java Serialization Static Fields Map
- Feb 24, 2011 Static member variables don't belong to any particular instance of a class. When you say - ClassA.num2 = num2;, you aren't accessing static variable num2 with any specific instance. You are accessing with class name and that is how static variables need to be accessed though in your case ( because both the argument and static variable bear the same name ).
- Given that the class doesn't have a no arg constructor, how can it be instantiated and the final field set? Some nasty black magic happens. There is a backdoor in the JVM that allows an object to be created without invoking any constructor. The fields of the new object are first initialized to their default values (false, 0, null, etc), and then the object.
- Java Custom Serialization. In most of cases, when you will customize java serialization, you will be writing the fields one by one – in a sequence. Its most common way to override default java serialization process. Let’s say, we have one User object and we want to customize it’s serialization process.
- What is Java Serialization, What is Deserialization, Only classes which implement Serializable can be serialized, Why Serializable is not implemented by Object, The transient and static fields do not get serialized, What is serialVersionUID and Why should we declare it, Customizing Serialization and Deserialization with writeObject and readObject methods, Stopping Serialization.
A static method can be marked transient which would normally be redundant, but you could use this to highlight static fields you don't want to serialize. You can call this method from your writeObject to save you having to do this for every field. Static fields are indeed not serialized by the default serializer. However, static transient fields can be detected via reflection. If someone writes its own serializer and he wants to also serialize static fields, then he might take the transient keyword in consideration and skip the serialization of that particular field.
Active5 years, 6 months ago
I have an class defining an immutable value type that I now need to serialize. The immutability comes from the final fields which are set in the constructor. I've tried serializing, and it works (surprisingly?) - but I've no idea how.
Here's an example of the class
Given that the class doesn't have a no arg constructor, how can it be instantiated and the final field set?
(An aside - I noticed this class particularly because IDEA wasn't generating a 'no serialVersionUID' inspection warning for this class, yet successfully generated warnings for other classes that I've just made serializable.)
amaidment
4,32533 gold badges4141 silver badges8181 bronze badges
mdmamdma49.9k1111 gold badges8181 silver badges117117 bronze badges
3 Answers
Deserialization is implemented by the JVM on a level below the basic language constructs. Specifically, it does not call any constructor.
Michael BorgwardtMichael Borgwardt
304k6767 gold badges442442 silver badges674674 bronze badges
Given that the class doesn't have a no arg constructor, how can it be instantiated and the final field set?
Some nasty black magic happens. There is a backdoor in the JVM that allows an object to be created without invoking any constructor. The fields of the new object are first initialized to their default values (false, 0, null, etc), and then the object deserialization code populates the fields with values from the object stream.
(Now that Java is open sourced, you can read the code that does this ... and weep!)
Stephen CJava Serializable Static Fields
Stephen C550k7777 gold badges639639 silver badges979979 bronze badges
Both Michael and Stephen gave you an excellent answer, I just want to caution you about
transient
fields.If default value (
Alexander PogrebnyakAlexander Pogrebnyaknull
for references, 0 for primitives ) is not acceptable for them after deserialization then you have to provide your version of readObject
and initialize it there.39.2k99 gold badges8686 silver badges114114 bronze badges
protected by om-nom-nomAug 8 '13 at 15:38
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged javaserializationfinal or ask your own question.
Active4 years ago
I just found out in Java you can declare a field 'static transient' - the compiler doesn't complain. This doesn't seem to be useful in any way since static fields are not serialized, as we all know.
But I wonder, is there actually a case where 'static transient' fields are useful?
python dudepython dude3,32988 gold badges3535 silver badges5252 bronze badges
2 Answers
Nope - you said it yourself, static fields aren't serialized.
Kinda weird that the compiler lets you do that though.
Peter CPeter C
In most cases, it is not useful. Static fields are indeed not serialized by the default serializer.
However,
static
transient
fields can be detected via reflection. If someone writes its own serializer and he wants to also serialize static fields, then he might take the transient
keyword in consideration and skip the serialization of that particular field.PS: This answer is posted for the sake of completeness, and is based on Peter Lawrey's comment. Credits to him.
Community♦
Java Serialization Static Fields In Word
MC EmperorMC EmperorSerialization Java Example
10.5k1212 gold badges5656 silver badges9292 bronze badges