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
tomex443
Posts: 3 Joined: Thu Aug 01, 2019 4:25 am
Contact:
Post
by tomex443 » Fri Aug 30, 2019 7:40 am
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: 10101 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Fri Aug 30, 2019 8:12 am
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:
Post
by tadite » Fri Nov 29, 2019 2:50 pm
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: 10101 Joined: Fri Sep 10, 2010 9:26 pm
Contact:
Post
by admin » Sat Nov 30, 2019 12:39 am
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 .
Users browsing this forum: No registered users and 8 guests