About Question enthuware.ocpjp.ii.v11.2.3508 :

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
tomex443
Posts: 3
Joined: Thu Aug 01, 2019 4:25 am
Contact:

About Question enthuware.ocpjp.ii.v11.2.3508 :

Post by tomex443 »

Code: Select all

  public void readObject(ObjectInputStream stream) throws IOException        
    throws IOException, ClassNotFoundException {           
    stream.defaultReadObject();               
    ld = LocalDate.of(2020,1,1);    
    }
This method don't even compile, because it has 2 throws signatures:
There should be only " throws IOException, ClassNotFoundException { " or " throws Exception {"

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

Re: About Question enthuware.ocpjp.ii.v11.2.3508 :

Post by admin »

You are right. There should be only one. It is irrelevant to the question though. Either one (but only one) of them would be fine.

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

tadite
Posts: 1
Joined: Fri Nov 29, 2019 2:34 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3508 :

Post by tadite »

Hello.

I'm think its typo here, for my run "The readObject method will be invoked before a Data object is deserialized." was indicated as green, but correct answer is "The readObject method will never be invoked." because readObject must be private. Am i right?

Code sample to proof it:

Code: Select all

public static void main(String[] args) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        Data data = new Data();
        data.id = 123;
        data.ld = LocalDate.now();

        oos.writeObject(data);

        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));

        Object data2 = ois.readObject();
        System.out.println(data2);
    }


    static class Data implements Serializable {
        int id;
        transient LocalDate ld;

        // won't be invoked
    public void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        System.out.println("I'm invoked");
        stream.defaultReadObject();
        ld = LocalDate.of(2020, 1, 1);
    }

        // will be invoked
//        private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
//            System.out.println("I'm invoked");
//            stream.defaultReadObject();
//            ld = LocalDate.of(2020, 1, 1);
//        }
    }

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

Re: About Question enthuware.ocpjp.ii.v11.2.3508 :

Post by admin »

Yes, you are right. It should be private.
Fixed.
thank you for your feedback,
Paul.
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 27 guests