• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static field Serialization

 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi....

I hav read from many articles that the static fields are not serialized and will not give the initialized value. I am not getting the same behavior from my program.


public class Client implements Serializable {
static float f = 10.0f;
code for serializing the Client instance.....
}

public Class Server {
Client c;
deserializing the Client object. saved it in c.

System.out.println(c.f)
}

f is comin 10.0 why???

f is static then why i gets serialized?? it should come 0.0 why 10.0 comin??

Plz Help

Any suggestion will be highly appreciated.

Thanks & Regards

Naseem Khan
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The value isn't serialized; it's initialized when the class is loaded in the server. Here's a better example: create an object in the client, then set the static variable to a different value, then serialize the object. Now deserialize the object in the server and check the static variable -- it will have the original value, not the changed value.

This has nothing to do with Threads -- I'll move it to I/O and Streams.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tx 4 replying....

U hav cleared my doubt. Now its clear that static fields are not serialized.

So is it ok to say that in the context of serialization, static fields come in between the transient and nonstatic fields.

As transient fields are not serialized. Even u can't get there initialized value at the time of deserialization.

with static fields, still it is not serialized but there initial value is copied.

with non static field, u can get there initial value + changed as well.

in a KISS format static lies in between transient and non static fields.



wat u think sir...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the truth is that exactly the same thing happens for transient and static fields: nothing. The initialization value isn't copied -- it's just the value that the static gets when the class is loaded. It gets that value from class loading, not from serialization.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u mean static value initialization talking place at the compilation time. Not at the run time. dats y u can get initialised value at the deserialized time even if static fields r not deserialized.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what a "ClassLoader" is, and what it does?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It loads the class in the JVM and dat will be at run time. Static variable will be initialized jst before class load.

Isn't it
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right.

OK, so that's all that's happening here. When an object is serialized, only the member data is serialized; the .class file still has to be available and still has to be loaded in the normal way. So the reason the static variable has its initial value is because the server loads the class, which initializes the variable, and then the object can be deserialized.

If you then changed the static variable in the server, and deserialized another instance of the same class, the changed value would not be affected -- it would stay changed. The deserialization wouldn't change its value.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really thank u for giving ur such valuable time. Now every thing is absolutely clear. The way u explained the thing waz really superb...

Thanks one again sir.

Take Care

Bye

Naseem Khan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!