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
Student
Posts: 53
Joined: Fri Sep 20, 2013 7:20 am
Contact:

About Question enthuware.ocpjp.v7.2.1620 :

Post by Student »

However, since f4 is being initialized as a part of class initialization, it will be initialized to the same value in another JVM. Thus, its value will be preserved.
What if in the intervening time period f4's declaration is changed from this:

public final static String f4 = "4";

to this:

public final static String f4 = "5";

? Will f4's value be preserved?

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 »

No, if you change the code then it won't be preserved. In fact, in that case, you may even get an exception while deserializing because the serialVersionUID might be different (not necessarily, but might be) after the code change. You have to check it out.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

disznoperzselo
Posts: 28
Joined: Fri Jan 02, 2015 12:13 pm
Contact:

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

Post by disznoperzselo »

The correct answer is that it won't compile because it tries to assign a String to d.f2 of type int. Read the text carefully on the exam.

ewebxml
Posts: 78
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post by ewebxml »

I selected c), but the explanation says for option b)

Code: Select all

public final static String f4 = "4";
However, since f4 is being initialized as a part of class initialization,
it will be initialized to the same value in another JVM.

General Rule:
Transient fields are not serialized.
static fields are not serialized.

Can I safely say as an exception to the rule:
Exception:
If a static field is being initialized as a part of class initialization,
it will be initialized to the same value in another JVM.

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 »

ewebxml wrote:I selected c), but the explanation says for option b)

Code: Select all

public final static String f4 = "4";
However, since f4 is being initialized as a part of class initialization,
it will be initialized to the same value in another JVM.

General Rule:
Transient fields are not serialized.
static fields are not serialized.

Can I safely say as an exception to the rule:
Exception:
If a static field is being initialized as a part of class initialization,
it will be initialized to the same value in another JVM.
I wouldn't call it an exception. static fields are not serialized. There is no exception to this rule. A static field that is initialized as a part of class initialization will be initialized when the class is loaded in any jvm. This part has nothing to do with serialization.
-Paul.
If you like our products and services, please help us by posting your review here.

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

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

Post by codecodecode67 »

if the line

Code: Select all

public static String f1;
was changed to

Code: Select all

public static String f1 =  "someString";
Would f1 also be initialized to "someString" in another JVM regardless of the fact that it's not declared final?

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 »

codecodecode67 wrote:if the line

Code: Select all

public static String f1;
was changed to

Code: Select all

public static String f1 =  "someString";
Would f1 also be initialized to "someString" in another JVM regardless of the fact that it's not declared final?
Yes. final has nothing to do with it.
If you like our products and services, please help us by posting your review here.

codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

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

Post by codecodecode67 »

Got it!

I'm sorry if this is an annoying question, but if line

Code: Select all

public static String f1;
was changed to

Code: Select all

public transient String f1 =  "someString";
Would f1 then be initialized to "someString" in another JVM regardless of the fact that it's not declared transient? - in my mind it shouldn't..?

Edit - some new information below
When a transient final field member is initialized to a constant expression as those defined in the JLS 15.28. Hence, field members declared this way would hold their constant value expression even after deserializing the object. I guess that is so because the value of the final field is actually a constant expression as described by the JLS.

Which leads me to think that the without the final modifier, the static field's constant value will not be preserved although it's a static field - it's not a constant field if it's not 'final'.

Please let me know your thoughts.

Thanks!

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 »

No, it will not be initialized to "someString" because f1 is transient and therefore the value will not be present in the serialized data.
Further, since constructors and instance initializers of a class are NOT executed when an object of that class is deserialized, f1 will not be given the value of "someString" automatically.

Here are the exact details of the steps that are taken by the JVM when it serializes and deserializes an object: https://docs.oracle.com/javase/8/docs/p ... alTOC.html
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

I think only f4 should be the answer.
Because if you say so then even those other static fields are preserved because even if you deserialize in another jvm those static fields will get the same default values which is null for non-primitive objects, false for boolean and likewise.
Even then if you say I'm wrong then I argue that still those static fields value will not be preserved because say if you have something like this:

Code: Select all

static int j = 5;
static
{
j = 6;
}
So in this case the static initializer blocks will be executed as soon as the class is loaded regardless of deserialization and that means the original value of j=5 will be lost and changed to 6 unlike in case of non-static variables for which initialization blocks are not executed in the classes which implement Serializable.

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: No registered users and 51 guests