CodeGym
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
matemate123
Level 32
Kraków
  • 15.02.2023
  • 34views
  • 4comments

I have one question

Question about the task Find the bugs
Java Core,  Level 10,  Lesson 10
Under discussion
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
15 February, 21:23useful
If you run the original code (after making Solution Serializable) you will see that nameB is being serialized while nameA is not. Now take this code as an example
public class Solution implements Serializable {
    public class B implements Serializable {

        private String nameB, nameA;


        public B(String nameA, String nameB) {
            this.nameA = nameA;
            this.nameB = nameB;
        }

        private void writeObject(ObjectOutputStream oos) throws IOException {
            oos.defaultWriteObject();

        }

        private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
            ois.defaultReadObject();
        }
    }

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

        Solution solution = new Solution();
        B b = solution.new B("B2", "C33");
        System.out.println("nameB: " + b.nameB + ", nameA: " + b.nameA);

        oos.writeObject(b);

        ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(arrayInputStream);

        B b1 = (B)ois.readObject();
        System.out.println("nameB: " + b1.nameB + ", nameA: " + b1.nameA);
    }
}
I removed the A class completely while putting the nameA field in the B class. If you run it you will see that both the fields have serialized properly. This is because serializable classes will have all their fields serialized and deserialized wiht just the default read/write object methods.
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
15 February, 21:34useful
The problem with the original code (after adding Serializable to Solution and a no args constructor to A) is that class A is not serializable so the defaultWriteObject() method is not serializing the nameA field that is part of the A class. You could fix this by implementing the Serializable interface on the A class, however the task conditions specifically state that you should not do this. This means that you are specifically going to need to write, and subsequently read, nameA in the proper methods. It looks like you have already figured this all out, hopefully I was able to explain it a little to make more sense. You do have one problem in your code in that the B constructor should still be the same:
public B(String nameA, String nameB) {
   super(nameA); // <- looks like this was removed
   this.nameA += nameA;
   this.nameB = nameB;
}
When creating new B objects the constructor with one argument in A should be called but that change in your constructor would call the no args constructor and be a potential bug. I suggest adding that line back.
+1
matemate123
Level 32 , Kraków, Poland
16 February, 08:26
Step after step it's more clearer. Thanks guys!
0
Thomas
Level 31 , Bayreuth, Germany
15 February, 21:16useful
to serialize nameB you either can go for read- writeObject or you use the defaultRead- WriteObject methods. Which one you chose does not matter here. Just use the same for reading and writing
+1
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.