Page 1 of 1

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Wed Jan 18, 2017 10:17 am
by admin
I am not sure I understand your point but the answer given in the question and the explanation are correct.

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Sun Aug 20, 2017 6:48 am
by rolandlensink
static fields are serialized, only a static field is serialized with the last value at the moment of serialization.

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Sun Aug 20, 2017 11:10 am
by admin
Where did you read that?

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Sun Aug 20, 2017 4:33 pm
by rolandlensink
I found out by experience ... try it yourself ...

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Mon Aug 21, 2017 1:42 am
by admin
Would you consider the possibility that you may be drawing wrong inference from whatever test program your wrote and ran?

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Tue Dec 25, 2018 12:33 am
by lennychapfuwa
for sure the answer is b. note that f1, f2, f3 and f4 will not be serialized, but only f5 will be serialized. however f1,f2 and f3 have been given new values which will be defaulted to the following at deserialization, f1 = null f2 = null, f3 = false. f4 will remain "4" and f5 will remain "5" at deserialization.

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Sun Mar 03, 2019 4:58 pm
by crazymind
Say we have "public static String f6 = "f6";" inside class Data, then use d.f6 = "f6 changed"; to change f6. Is f6 changed? I understand that static is not serialized and JVM load class with f6's initial value. So I guess it is not changed. And is f1 print null after deserialization?

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Sun Mar 03, 2019 9:40 pm
by admin
What happened when you tried it out?
Post complete code.

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Sun Dec 08, 2019 6:18 am
by Bhaskar
So in short can it be said that, since f1, f2 and f4 are static, their values will be maintained after de-serialization only if performed in the same JVM, and If de-serialized in another JVM, the values of f1,f2,f3 will be lost and only f4 will be maintained because of class initialization? Please verify.

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Sun Dec 08, 2019 11:38 am
by admin
That's correct.

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Wed Mar 31, 2021 3:38 pm
by jme_chg
So just to confirm:


If it's an instance variable
  • if initialized, it will always have same value when serialized and deserialized, regardless of whether in same JVM or not.
  • if not initialized, it will always take default value when serialized and deserialized, regardless of whether in same JVM or not.

If it's a transient variable, it will always take default value when serialized and deserialized, regardless of whether initialized/uninitialized or whether in same JVM or not.


If it's a static variable then:
  • if initialized, it will always have same value when serialized and deserialized, only when in a different JVM.
  • if not initialized, it will always take default value when serialized and deserialized, regardless of whether in same JVM or not.

If variable is both static and transient, apply static rules.


?

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Thu Apr 01, 2021 1:50 am
by admin
It is hard to confirm such things because there are too many ifs in there. But in general, it looks ok. I wouldn't recommend anyone to bank on the above rule book though.

Re: About Question enthuware.ocpjp.v7.2.1620 :

Posted: Thu Apr 01, 2021 11:17 am
by jme_chg
You're right, so many ifs that I made a mistake,

I think it can simply be generalised to:

INSTANCE/STATIC VARIABLES:
After both serialized/deserialized, same value if initialised, default value if not initialised (regardless of JVM).

TRANSIENT VARIABLES:
After both serialized/deserialized, always default value (regardless of JVM).

If variable both static and transient, apply static rule.

To me, it looks like instance/static variables behave the same - the only difference is that for statics, they don't get serialized in between...

Correct me if I'm wrong please.