That's only true if you also remove the line that increments si (i.e., boo.si++).However, if you run the program again with just the deserialization part, you will see that si is 20 and not 21.
About Question enthuware.ocpjp.v7.2.1701 :
Moderator: admin
-
- Posts: 17
- Joined: Tue Dec 18, 2012 7:54 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1701 :
In the explanation for the correct answer, you indicate:
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
Yes, but that line is not a part of the deserialization anyway.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
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.
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.
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
i see. thank u so much.
-
- Posts: 5
- Joined: Fri Jan 17, 2014 6:08 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
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).
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
No, it is correct. Please go through the explanation. It explains why it prints 21.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).
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
If the deserialization part is run in another application (i.e., not in this main()), boo.si should be 20. Is it right?
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
Yes, that is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Thu May 08, 2014 5:27 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
please help me. my exam is tomorow
After the deserialization there is no statement like "boo.si++;" then how it becomes 21?
After the deserialization there is no statement like "boo.si++;" then how it becomes 21?
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
Did you read the explanation? It explains why it becomes 21.
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Dec 04, 2016 6:29 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
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
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
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
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.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 ?
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.
-
- Posts: 2
- Joined: Sun Dec 04, 2016 6:29 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
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:
Now it's all clear! Thanks again, bye
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);
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
Why??Therefore, if you run the program again with just the deserialization part, you will see that si is 20 and not 21.
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
What does the complete explanation say?
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
Here is the complete explanation say
My question is: it first saysRemember 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.
But then it says:So when you deserialize an instance of Boo, Boo.si is not affected and is not reset to 20.
Why does si become 20 again if you run the program again with just the deserialization part ?Therefore, if you run the program again with just the deserialization part, you will see that si is 20 and not 21.
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
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.
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
admin wrote: ↑Fri Mar 08, 2019 10:02 pmIn 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!
-
- Posts: 34
- Joined: Mon Oct 30, 2023 11:55 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
I am really sorry, after all these explanations I do not understand it fully. Please explain. My question is:
if I write 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?
if I write
Code: Select all
boo = null;
Could you please explain this?
-
- Site Admin
- Posts: 10095
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1701 :
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.
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.
Who is online
Users browsing this forum: Google [Bot] and 6 guests