Answer the question
In order to leave comments, you need to log in
Should an object in Java serialize itself?
Good afternoon, dear Java gurus.
I have a class which contains a collection of objects. There is a static function that serializes an object of this class into an xml file. I wanted to remake this function into a class method, but then I wondered if it was right. Can you please tell me if the object methods can serialize the object itself and then get it back from the file? Or is it more correct to create some external class that will serialize an object of the original class?
Answer the question
In order to leave comments, you need to log in
1 class should solve 1 task serialization is already the second task, and it should be in a separate class. As an option in the decorator of the original class.
You can do it in different ways. It all depends on your architecture.
I would advise you to create an interface, and inherit it in the right class.
public interface IXmlSerialize {
void serializeToXml(String fileName);
void deserializeFromXml(String fileName);
}
if you put serealization \ deserialization into a class object, then for deserialization the object must rewrite itself, which the language will not allow, so it will be possible to deserialize either into one of the fields of the same class or into a separate object.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question