About Question enthuware.ocpjp.v7.2.1701 :

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 » Wed Aug 07, 2013 3:10 pm

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: 8186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Thu Aug 08, 2013 11:48 am

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 » Sun Sep 29, 2013 6:36 am

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: 8186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Sep 29, 2013 8:42 am

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 » Mon Sep 30, 2013 12:15 am

i see. thank u so much.

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

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

Post by mdraisma » Fri Feb 21, 2014 4:46 am

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: 8186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Feb 21, 2014 11:31 am

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 » Sun Mar 09, 2014 3:41 am

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: 8186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Mar 09, 2014 5:32 am

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 » Sat May 31, 2014 10:37 am

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: 8186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat May 31, 2014 8:08 pm

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 » Sun Dec 04, 2016 6:44 am

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: 8186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Dec 04, 2016 10:48 pm

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 » Mon Dec 05, 2016 5:55 am

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 » Wed Mar 06, 2019 3:44 pm

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: 8186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Wed Mar 06, 2019 7:54 pm

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 » Fri Mar 08, 2019 4:29 pm

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: 8186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » 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.
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 » Fri Mar 08, 2019 10:22 pm

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!

Post Reply

Who is online

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