CodeGym /Courses /Java Core /Practice with serialization

Practice with serialization

Java Core
Level 10, Lesson 4
Available

"Hello, Amigo!"

6
Task
Java Core, level 10, lesson 4
Locked
How do you serialize?
How do you serialize?
6
Task
Java Core, level 10, lesson 4
Locked
How do you serialize CodeGym?
How do you serialize CodeGym?
12
Task
Java Core, level 10, lesson 4
Locked
How do you serialize Singleton?
How do you serialize Singleton?
6
Task
Java Core, level 10, lesson 4
Locked
How do you serialize static fields?
How do you serialize static fields?
6
Task
Java Core, level 10, lesson 4
Locked
How do you serialize something of your own?
How do you serialize something of your own?
Comments (13)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Evgeniia Shabaeva Level 42, Budapest, Hungary
8 November 2024
To understand the third task a little better (e.g. to get some info how to use the readResolve() method), I'd recommend this article.
catalin1989 Level 37, Romania
1 December 2023
Liked the task about serialization and deserialization of a Singletone class.
catalin1989 Level 37, Romania
30 November 2023
Finally, some easy tasks. I fried my brain doing the previous tasks :))).
Gellert Varga Level 23, Szekesfehervar, Hungary
24 November 2021
About Singleton task and readResolve() method: "readObject() is an existing method in ObjectInputStream class. While reading object at the time of deserialization readObject() method internally check whether the class object which is being deserialized having readResolve() method or not. If readResolve() method exists then it will invoke it and return the same instance." "So the intent of writing readResolve() method is a good practice to achieve pure singleton design pattern where no one can get another instance by serializing/deserializing." "readResolve is used for replacing the object read from the stream. ...enforcing singletons: when an object is read, replace it with the singleton instance. This ensures that nobody can create another instance by serializing and deserializing the singleton." "Similary we have writeReplace() method for ObjectOutputStream." Source: https://stackoverflow.com/questions/1168348/java-serialization-readobject-vs-readresolve Summarized in a nutshell: use readResolve() method to override what readObject() returns. And how to code it: See the link he suggests below: Minh Phạm Đức, Level 22 , Cedar Falls, 20 April 2019, 12:37
Mateusz Level 29, Poland
19 September 2020
these easy exercises are suuuuuper easy compared to the exercises from the previous section
Mateusz Level 29, Poland
19 September 2020
interesting article on serializing singletons: https://www.geeksforgeeks.org/prevent-singleton-pattern-reflection-serialization-cloning/
Petros Level 23, Columbia, United States
19 September 2020
Don't understand half the code or what I'm doing but like Johaness said, on we go! LOL
Johannes Level 27, Centurion, Pretoria, South-Africa
22 April 2020
The Singleton task makes absolutely ZERo sense, LOL!! On we go!
Fadi Alsaidi Level 34, Carrollton, TX, USA
31 May 2020
Man you can say that again like a 1000 times. I dont get it. why even creat the method? ..we can just return the same getInstance() from the singleton class!!!!??? even better, why bother with creating a deserialized method and read the byts and go all around the world to return the same freaking object. just make it return singleton getInstance() method.. I dont get this and it's driving me nuts. If there are anyone with enough kindness and understanding explain this, it would be wonderful
Daniel Walbolt Level 22, Waterville, United States
9 August 2020
Hmm, complicated and vague. Let me TRY to explain it in my own understanding because I don't even think it is exactly correct. Saving and loading is not for one program's instance to do. It's for multiple. EACH and EVERY time you run a Java application, you create a new program instance, and likewise a new runtime environment (JRM). The save and load (serialization and deserialization) are used for communicating the exact objects used from previous generations of programs from the same blueprint (classes). A Singleton class is ONLY a Single instance of that class in that program instance, it is different in other programs. Getting the Singleton and all its data from the previous program is useful in order to maintain the same interaction between classes possible before. However, in our case, there is little data stored, and each creation of the Singleton class is subjectively the same. There are little differences that could possibly emerge, but it's the DESIGN PATTERN of saving and loading properly that matters the most here.
Minh Phạm Đức Level 22, Cedar Falls, United States
20 April 2019

https://javarevealed.wordpress.com/tag/singleton-and-serialization/
Singleton and Serialization
Satya Vath Level 31, Vijayawada, India
22 April 2019
Thank You
Simphiwe Level 33, Johannesburg, South Africa
6 April 2023
Good additional read