Page 1 of 1

About Question enthuware.ocpjp.v7.2.1701 :

Posted: Wed Aug 07, 2013 3:10 pm
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++).

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

Posted: Thu Aug 08, 2013 11:48 am
by admin
Yes, but that line is not a part of the deserialization anyway.

HTH,
Paul.

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

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

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

Posted: Sun Sep 29, 2013 8:42 am
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.

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

Posted: Mon Sep 30, 2013 12:15 am
by icepeanuts
i see. thank u so much.

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

Posted: Fri Feb 21, 2014 4:46 am
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).

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

Posted: Fri Feb 21, 2014 11:31 am
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.

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

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

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

Posted: Sun Mar 09, 2014 5:32 am
by admin
Yes, that is correct.

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

Posted: Sat May 31, 2014 10:37 am
by kashyapa
please help me. my exam is tomorow

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

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

Posted: Sat May 31, 2014 8:08 pm
by admin
Did you read the explanation? It explains why it becomes 21.
Paul.

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

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

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

Posted: Sun Dec 04, 2016 10:48 pm
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.

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

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

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

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

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

Posted: Wed Mar 06, 2019 7:54 pm
by admin
What does the complete explanation say?

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

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

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

Posted: Fri Mar 08, 2019 10:02 pm
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.

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

Posted: Fri Mar 08, 2019 10:22 pm
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!

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

Posted: Thu Jan 04, 2024 4:51 am
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?

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

Posted: Fri Jan 05, 2024 3:17 am
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.