About Question enthuware.ocpjp.v8.2.1620 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
jpm_cr7
Posts: 1
Joined: Sun Apr 17, 2016 9:58 am
Contact:

About Question enthuware.ocpjp.v8.2.1620 :

Post by jpm_cr7 »

f1 is static variable. we have set d.f1 to "f1". Since it is static it will not be serialised but it will retain its value to f1. Since the question asks about the fields which will retain its value, the answer should include f1 also.

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

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

Post by admin »

The problem statement says, "...serialized and deserialized in another JVM".
The static field will not be assigned "f1" in another JVM if you just deserialize the previously serialized object.

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

karlharb
Posts: 2
Joined: Wed Apr 06, 2016 12:54 pm
Contact:

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

Post by karlharb »

I guess the fact that the source code is shown implies that it doesn't apply in this case, but in principle, when the object is deserialized in another JVM, a different version of the class could be loaded, potentially setting f4 to a different value?

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

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

Post by admin »

Yes, it is possible. You should read about serialVersionUid. Here are a couple of good links:
http://www.jusfortechies.com/java/core- ... ionUID.php
and
http://stackoverflow.com/questions/2857 ... d-i-use-it
If you like our products and services, please help us by posting your review here.

ffdeztrujillo
Posts: 1
Joined: Sat Jul 09, 2016 2:04 am
Contact:

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

Post by ffdeztrujillo »

Hi!, in the explanation says:
...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 same as the one initialized by the code.
However, in question number enthuware.ocpjp.v8.2.1701 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
I'm confused about how f4 is being initialized.
Thank you!

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

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

Post by admin »

Both are talking about different things and are correct. static blocks and static field initializations are not invoked during desrialization but they are invoked during the loading of the class by the JVM.Every class has to be loaded first by the JVM whether an instance is created or not. Loading of the class has nothing to do with serialization/deserialization or creation of an instance.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

sharpmario
Posts: 4
Joined: Sun Jul 31, 2016 12:21 pm
Contact:

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

Post by sharpmario »

I'm confused as well

So, why the answer of enthuware.ocpjp.v8.2.1701 is not
10 21
10 21
???

If the attribute 'ti' is being initialized as a part of class initialization?

I don't see the difference... I missed this question because I didn't think that f4 would recieve the value.

Thanks

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

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

Post by admin »

1. Class initialization happens just once in the life time of the program (actually the lifetime of the class loader, but not important at this point). Not every time when you create an object. It has nothing to do with serialization.
2. Instance initialization happens every time you create an instance using the new keyword. It doesn't happen when you create an instance through deserialization.

Now, in case of 2.1701, ti is an instance field, which would be initialized to 10 every time you create Boo instance using the new keyword. But in the given code, you are creating Boo instance using new only the first time. The second time you are creating Boo instance through deserialization. Therefore, for the second Boo instance ti will not get a value of 10 through the initialization part. It will, however, get a value that was present in the serialized data stream. Since ti is transient, its original value of 10 was never stored in the serialized data stream when you serialized the object. That is why, when you deserialize it back from the stream, you will get 0 for ti.

Compare this with 2.1620. The variable f5 is not transient. In this case, when you serialize the object, the value of f5 (which was initialized to "5" when you created the Data object using new keyword), will be stored in the data stream. When you create the Data object again by deserializing the data stream, f5 will get back that value of "5" that was there in the stream. This has nothing to do with the instance initialization part, which never happened for this Data object.

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

lenalena
Posts: 56
Joined: Tue Feb 21, 2017 4:24 pm
Contact:

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

Post by lenalena »

It also took me a while to wrap my mind around this question and why f4 is correct. I think the issue is with the wording of the question. The question asks which values will be preserved when the object is serialized and then deserialized. I think for many such wording means "which value is preserved during the serialization/deserialization process". And if we read the question like that - then f4 is NOT correct - it's value is not preserved during serialization.

However, if the question is meant to ask which field will have a value after deserialization is complete - then f4 is correct, because it will have a value which will be loaded from the class version at the target JVM during deserialization - and NOT from the stream that was used to deserialize.

Just my 2c.

Cannelids
Posts: 25
Joined: Thu Jun 01, 2017 2:50 pm
Contact:

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

Post by Cannelids »

I think I agree with lenelena. I knew static fields that had not been reassigned would have the same value, but had to guess whether the word "preserved" implied "by serialization"...and guessed "yes" incorrectly :?

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

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

Post by admin »

I have updated the problem statement to, "Which fields of the above class will be have the same values ..." instead of "preserved". That should make it very clear :)

thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

Cannelids
Posts: 25
Joined: Thu Jun 01, 2017 2:50 pm
Contact:

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

Post by Cannelids »

Thanks Paul (though I realise the real exam might have ambiguities anyway 8-) )

ssszzz
Posts: 13
Joined: Wed Jun 14, 2017 1:56 pm
Contact:

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

Post by ssszzz »

For me it is not clear yet about this 'f4'
Which fields of the above class will be have the same values ...
What if the code was changed?
The following snippet shows it is okay if we just change constant value:

Code: Select all

public class Test35 {

    public static void main(String... strings) throws IOException, ClassNotFoundException {
        String file = "./test.data";
        /*Data d = new Data();
        d.f1 = "f1";
        d.f2 = 2;
        d.f3 = true;
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
            out.writeObject(d);
        }*/
        Data d2 = null;
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
            d2 = (Data) in.readObject();
        }
        System.out.println(d2);
    }
}

class Data implements java.io.Serializable {
    public static String f1;
    public static transient int f2;
    public transient boolean f3;
    public final static String f4 = "442";
    public String f5 = "5";
}
But if we comment out line 'public final static String f4' or change variable name then we get exception while deserialization about 'serialVersionUID' as expected.
Tested on build 1.8.0_45-b15

But, of course,
the above class
should meant that this class remains untouched, yeah?

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

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

Post by admin »

That is correct. No change in class code.
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 21 guests