About Question enthuware.ocpjp.v7.2.1620 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

rolandlensink
Posts: 7
Joined: Thu Mar 16, 2017 7:44 am
Contact:

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

Post by rolandlensink »

static fields are serialized, only a static field is serialized with the last value at the moment of serialization.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Where did you read that?
If you like our products and services, please help us by posting your review here.

rolandlensink
Posts: 7
Joined: Thu Mar 16, 2017 7:44 am
Contact:

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

Post by rolandlensink »

I found out by experience ... try it yourself ...

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

lennychapfuwa
Posts: 2
Joined: Tue Dec 18, 2018 6:37 am
Contact:

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

Post 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.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

What happened when you tried it out?
Post complete code.
If you like our products and services, please help us by posting your review here.

Bhaskar
Posts: 19
Joined: Fri Aug 02, 2019 7:04 am
Contact:

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

Post 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.
Last edited by Bhaskar on Sat Dec 21, 2019 1:42 am, edited 1 time in total.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

That's correct.
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

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

Post 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.


?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 17 guests