Page 1 of 1

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

Posted: Fri Aug 30, 2019 7:40 am
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 {"

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

Posted: Fri Aug 30, 2019 8:12 am
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!

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

Posted: Fri Nov 29, 2019 2:50 pm
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);
//        }
    }

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

Posted: Sat Nov 30, 2019 12:39 am
by admin
Yes, you are right. It should be private.
Fixed.
thank you for your feedback,
Paul.