How to deserialize an object?

I have a class called Flight. When I create an instance, the Flight class instantiates another class called SitChart, and SitChart also instantiates another class, etc. Etc.

public class Flight implements Serializable
{

    SeatingChart sc = new SeatingChart(); seating 
    //WaitingList wl = new WaitingList();
}

public class SeatingChart extends ListAndChart implements PassengerList, Serializable
{
    public static final int NOT_FOUND = 42;

    Passenger [] pass = new Passenger[40];
}

public class Passenger implements Serializable
{

    private String firstName, lastName, fullName;

    public String getName()
    {   
        fullName = firstName + " " + lastName;
        return fullName;
    }
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
}

I have another method in another class that deserializes an object stored on disk

public void actionPerformed(ActionEvent evt)
            {  
                Serialization.deserialize(sw101); <--- sw101 is a Flight object
                .
                .
                .
            }

//code for deserialization
public static void deserialize(Flight sw101)
    {
        String filename = "serialized.ser";

        sw101 = null;

        FileInputStream fis = null;
        ObjectInputStream in = null;

        try
        {
            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            sw101 = (Flight)in.readObject();
            System.out.println("sw101" + sw101.toString());
            in.close();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        catch(ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }   
    }

My question is, when I assign sw101 a serialized object, all sw101 created at the beginning, like the sitChart sc object, also gets everything that is saved in the file, without any action, if all objects implement the Serializable interface? If so, why is my code not working? what am I doing wrong?

+3
source share
2 answers

It looks like you are trying to return through a reference parameter (background C / C ++?)

Java. ( ) .

sw101=null;

, .

deserialize .

( Java , )

+2

java .. - . sw101 .

, , .

: , java

0

All Articles