Can non-static member classes (Local classes) have static members? What are the environment variables do we neet to set to run Java? Can you serialize static fields of a class? What is the difference between declaring a variable and defining a variable? Where can we use serialization? What modifiers are allowed for methods in an Interface? I appreciate this question b/c you can easily argue (as the answerer does) that static fields do not belong to any instance. However, once can also conversely argue that static fields belong to every instance if the Type and instances of a Type philosophy in.NET is not properly understood. Mainly b/c it is precisely that - a philosophy. Java Serialization tutorial - Java object serialization is used to persist Java objects to a file, database, network, process or any other system. Serialization flattens objects into an ordered, or serialized stream of bytes. The ordered stream of bytes can then be read at a later time, or in another environment, to recreate the original objects. 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.
Static variables belong to a class and not to any individual instance. The concept of serialization is concerned with the object’s current state. Only data associated with a specific instance of a class is serialized, therefore static member fields are ignored during serialization. Transient Variable. In this tutorial, we take a closer look at how to implement customized serialization in Java using the Externalizable interface. (except static and transient fields).
Java Serialization Static Fields Video
Active8 years, 7 months ago
When I serialize an object I want to also take a 'snapshot' of the static fields.Is the following code a good practice ?
Processor: Pentium 3 @ 1 GHz. Chess masters download. Memory: 256 Mb. Hard Drive: 1.2 Gb free.
(notice that in order to set the static field you have to go through an instance )
Yaron LeviYaron Levi4,79999 gold badges5656 silver badges103103 bronze badges
4 Answers
Maybe you could implement the dedicated
private void writeObject(ObjectOutputStream out) throws IOException;
method to customize your serialization when you write your objects. Then you have the private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
when you have to deserialize your objects. This way you can customize your serialization/deserialization process.
![Java serialization static fields video Java serialization static fields video](/uploads/1/2/4/8/124857081/895910346.jpg)
See http://java.sun.com/developer/technicalArticles/Programming/serialization/.
reefreef1,29322 gold badges2020 silver badges3434 bronze badges
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 ).When accessing with class name, it makes clearer to the reader that it is a static variable. Had if the passing argument bear a different name then,
MaheshMaheshnum2 = numX;
would have been correct. But this doesn't say to the reader whether num2
is a static variable or a normal class variable.28.9k1616 gold badges7474 silver badges104104 bronze badges
I don't think it's a good practice. Remember that static members belong globally to the class, and not to a particular instance.
Consider the case of serializing two ClassA instances, with different values of the static member. The serialized form will indeed indicate the two versions of the static data. However, after deserializing the two instances, only one value of the static member will hold (the last one being deserialized).
In other words, you make the value of the static member dependent on the deserialization order.
Eyal SchneiderEyal Schneider19.6k44 gold badges4040 silver badges7272 bronze badges
A static value is not supposed to be serialized. You may wanna reconsider your design if you really need to serialize static value.
Even if you do serialize like the way you have done, that will cause lots of problems for you. The value may be different for each serialized object.
Which value you will use when you deserialize? You can't decide know which one is the correct one.
fmucarfmucar12.7k22 gold badges3939 silver badges4848 bronze badges