About Question enthuware.ocpjp.v7.2.1620 :
Moderator: admin
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
I am not sure I understand your point but the answer given in the question and the explanation are correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Thu Mar 16, 2017 7:44 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
static fields are serialized, only a static field is serialized with the last value at the moment of serialization.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
Where did you read that?
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Thu Mar 16, 2017 7:44 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
I found out by experience ... try it yourself ...
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
Would you consider the possibility that you may be drawing wrong inference from whatever test program your wrote and ran?
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue Dec 18, 2018 6:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
What happened when you tried it out?
Post complete code.
Post complete code.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Fri Aug 02, 2019 7:04 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
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.
Last edited by Bhaskar on Sat Dec 21, 2019 1:42 am, edited 1 time in total.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
That's correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 07, 2021 6:30 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
So just to confirm:
If it's an instance variable
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 variable is both static and transient, apply static rules.
?
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.
?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 07, 2021 6:30 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1620 :
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.
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.
Who is online
Users browsing this forum: Bing [Bot] and 116 guests