About Question enthuware.ocpjp.v7.2.1701 :

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

Moderator: admin

Post Reply
jklb
Posts: 17
Joined: Tue Dec 18, 2012 7:54 pm
Contact:

About Question enthuware.ocpjp.v7.2.1701 :

Post by jklb »

In the explanation for the correct answer, you indicate:
However, if you run the program again with just the deserialization part, you will see that si is 20 and not 21.
That's only true if you also remove the line that increments si (i.e., boo.si++).

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

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

Post by admin »

Yes, but that line is not a part of the deserialization anyway.

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

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

The following explanation is confusing me.

1) "Remember that transient fields and static fields are never serialized."
2) "The class Boo is already loaded and so the static int si preserves its value. "

What does it exactly mean by "The class Boo is already loaded"? Why the static int si can preserve its value while static fields are never serialized?

Could you help me out? Thanks.

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

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

Post by admin »

Class Boo is loaded as soon as the code refers to the class (here, it happens at Boo boo = new Boo(); ), and so the static int si is initialized to the value given in the class code i.e. 20 and then it is incremented to 21 because of boo.si++;. This part has nothing to do with serialization. So when you deserialize an instance of Boo, Boo.si is not affected and is not reset to 20.

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

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

i see. thank u so much.

mdraisma
Posts: 5
Joined: Fri Jan 17, 2014 6:08 am
Contact:

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

Post by mdraisma »

The explanation is clear, but in the review the wrong answer is marked as the right one (after deserialize: 0 21, instead of 0 20).

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

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

Post by admin »

mdraisma wrote:The explanation is clear, but in the review the wrong answer is marked as the right one (after deserialize: 0 21, instead of 0 20).
No, it is correct. Please go through the explanation. It explains why it prints 21.
If you like our products and services, please help us by posting your review here.

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

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

Post by icepeanuts »

If the deserialization part is run in another application (i.e., not in this main()), boo.si should be 20. Is it right?

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

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

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post by kashyapa »

please help me. my exam is tomorow

After the deserialization there is no statement like "boo.si++;" then how it becomes 21?

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

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

Post by admin »

Did you read the explanation? It explains why it becomes 21.
Paul.
If you like our products and services, please help us by posting your review here.

someone
Posts: 2
Joined: Sun Dec 04, 2016 6:29 am
Contact:

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

Post by someone »

Hi, I've read the explanation and all this discussion but I still don't understand the behavior of si:
In the explanation is written that static fields (like si) are never serialized, so when I serialize boo I won't write on the file the current value (21) of si. Then, when i deserialize boo, considering i haven't serialized the value of the static field si, I will assign to si his default value (zero). So why I see 21 if boo is currently pointing to the desialized object (and not to the one created before the serialization)?
In addition, why shoud I see si = 20 if I run only the deserialization part, if the static fields are never serialized and field initialization of the class being deserialized are also not invoked? Shouldn't I see zero (default value) also in this case :?: :|

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

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

Post by admin »

someone wrote:Then, when i deserialize boo, considering i haven't serialized the value of the static field si, I will assign to si his default value (zero).

In addition, why shoud I see si = 20 if I run only the deserialization part, if the static fields are never serialized and field initialization of the class being deserialized are also not invoked? Shouldn't I see zero (default value) also in this case ?
It is true that static fields are not serialized. But they will be initialized at the loading of loading of the class by the JVM. Loading of a class happens for every class and only once. It has nothing to do with serialization or deserialization process.
That is why, si will be initialized to 20 when class Boo is loaded by the JVM. Now, when you serialize and deserialize an object of class Boo, it has no affect on the static fields. As you know, there is only one copy of a static variable of a class irrespective of how many instances of that class are there, when you do si++, you will see the incremented value i.e. 21 even in the instances that you got using deserialization.

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

someone
Posts: 2
Joined: Sun Dec 04, 2016 6:29 am
Contact:

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

Post by someone »

Thank you very much Paul! (also for the simulator, the best buy!)
So, only the static fields are initialized when the JVM loads (without create one) a class!!
Just a test I have done using only the deserialization part:

Code: Select all

Boo boo = null;
Object o = (Boo) is.readObject();  //here JVM loads the class initializing the static filed
System.out.println(((Boo)o).ti + " " + ((Boo)o).si);
Now it's all clear! Thanks again, bye

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

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

Post by crazymind »

Therefore, if you run the program again with just the deserialization part, you will see that si is 20 and not 21.
Why?? :shock:

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

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

Post by admin »

What does the complete explanation say?
If you like our products and services, please help us by posting your review here.

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

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

Post by crazymind »

admin wrote:
Wed Mar 06, 2019 7:54 pm
What does the complete explanation say?
Here is the complete explanation say
Remember that transient fields and static fields are never serialized. Constructor, instance blocks, and field initialization of the class being deserialized are also not invoked. So, when boo is deserialized, the value of ti is set to 0. The class Boo is loaded as soon as the code refers to the class (here, it happens at Boo boo = new Boo(); ), and so the static int si is initialized to the value given in the class code i.e. 20 and then it is incremented to 21 because of boo.si++;. This part has nothing to do with serialization. So when you deserialize an instance of Boo, Boo.si is not affected and is not reset to 20. Therefore, if you run the program again with just the deserialization part, you will see that si is 20 and not 21.
My question is: it first says
So when you deserialize an instance of Boo, Boo.si is not affected and is not reset to 20.
But then it says:
Therefore, if you run the program again with just the deserialization part, you will see that si is 20 and not 21.
Why does si become 20 again if you run the program again with just the deserialization part ?

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

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

Post by admin »

In the given code as given, you will see a statement boo.si++; this statement increments boo.si to 21.

You are not paying attention to this statement of the explanation, "The class Boo is loaded as soon as the code refers to the class (here, it happens at Boo boo = new Boo(); ), and so the static int si is initialized to the value given in the class code i.e. 20 and then it is incremented to 21 because of boo.si++;. This part has nothing to do with serialization. "

But if you just run the deserialization part of the code, boo.si++; will not be there to increment si to 21. That is why it will remain 20.

These are two different situations.
If you like our products and services, please help us by posting your review here.

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

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

Post by crazymind »

admin wrote:
Fri Mar 08, 2019 10:02 pm
In the given code as given, you will see a statement boo.si++; this statement increments boo.si to 21.

You are not paying attention to this statement of the explanation, "The class Boo is loaded as soon as the code refers to the class (here, it happens at Boo boo = new Boo(); ), and so the static int si is initialized to the value given in the class code i.e. 20 and then it is incremented to 21 because of boo.si++;. This part has nothing to do with serialization. "

But if you just run the deserialization part of the code, boo.si++; will not be there to increment si to 21. That is why it will remain 20.

These are two different situations.


Oh, I see. I am overthinking it. Thanks!

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

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

Post by Tester »

I am really sorry, after all these explanations I do not understand it fully. Please explain. My question is:
if I write

Code: Select all

boo = null;
after "os.close();" this mean that JVM has to collect object Boo and variable "boo" does not have any link to existing object. After that Object is deSerialized and "boo.si" is 21!
Could you please explain this?

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

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

Post by admin »

No, why would the JVM gc the object pointed to by boo after os.close()? We are not setting boo to null after this line.

It will be GCed after the line boo = (Boo) is.readObject(); because boo is now pointing to a new Boo object (which was created by deserialization).

You are probably confused between static and instance fields. si is a static variable and you can access it using Boo.si and also using boo.si. The code uses boo.si just to confuse you. That is why even when the Boo object is GCed, Boo.si's value of 21 still stays.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests