O
O
Oleg Carp2020-08-04 16:35:49
Java
Oleg Carp, 2020-08-04 16:35:49

How to serialize a class with a nested object of another class?

I want to store an object of this class

public class District implements Serializable {

    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    private Quarter quarter;

    public District(String name, int age, Quarter quarter) {
        this.name = name;
        this.age = age;
    }

}


That is, it is necessary to put both the District object itself and the nested Quarter object (and the necessary fields in it too) into one file. So far, my attempts only result in the saved fields District, and Quarter is null . Maybe someone knows useful resources or libraries.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Carp, 2020-11-11
@Rebel-Cat

In general, there was some kind of bug IDE rebooted and everything worked

Y
Yerlan Ibraev, 2020-08-05
@mad_nazgul

IMHO now serialization of objects is done via JSON. :-)
For example gson is a convenient library for working with JSON.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question