Categories: java, arrays, xml, object, serializable

Save Array of 2nd Object with Java Serializable

1 answer

i've got a class called "Settings" which has an array of "UniverseOut".

private static final long serialVersionUID = 2929431155275389116L;  private String projectName = "last-project";  private UniverseOut[] universeOut = new UniverseOut[unicastLimit]; 

When i change the "projectName" in the settings class via a command, it changes it and when i save the project, and restart the application it will read the name perfectly without any mistakes.

But the UniverseOut Array isnt being saved at all, it just says that the array is from that type but the values/object itself isnt saved.

public class UniverseOut implements Serializable {  /**  *   */ private static final long serialVersionUID = -1326425548448853224L; private static final int max_ip = 10; private static String[] ip = new String[max_ip];  } 

And this is the code how i save the objects as XML Files:

        String path = System.getProperty("user.dir");         FileOutputStream fos = new FileOutputStream(path + "\\" + projectname + ".project");         XMLEncoder xml = new XMLEncoder(fos);         xml.setExceptionListener(new ExceptionListener() {             public void exceptionThrown(Exception e) {                 System.out.println("Exception! :" + e.toString());             }         });         xml.writeObject(getSettings());         xml.close();         fos.close();         fos = new FileOutputStream(path + "\\last-project.project");         xml = new XMLEncoder(fos);         xml.writeObject(getSettings());         xml.close();         fos.close(); 

The file is being created and looks like this:

<?xml version="1.0" encoding="UTF-8"?> <java version="13.0.2" class="java.beans.XMLDecoder">  <object class="preferences.Settings" id="Settings0">   <void property="projectName">    <string>lol</string>   </void>   <void property="universeOut">    <void index="0">     <object class="preferences.UniverseOut"/>    </void>    <void index="1">     <object class="preferences.UniverseOut"/>    </void>   </void>  </object> </java> 

And im pretty confident that this array should contain some stuff, but it isnt saved.

Received answer to this question:
The best answer according to the author of the question:

If UniverseOut does not have any non-static fields to be serialized, then there's nothing to be saved, as statics are implicitly transient.