Java 为什么我的;马歇尔勒“;必须是;静态;?

Java 为什么我的;马歇尔勒“;必须是;静态;?,java,serialization,deserialization,marshalling,unmarshalling,Java,Serialization,Deserialization,Marshalling,Unmarshalling,我试图在它们自己的类中“marshall”和“unmarshall”对象,并使用“Marshaller” 主要方法: public class Main { public static void main(String[] args) { new Something(); } } 将生成要marshall或unmarshall的实例的“Something”类: import java.io.File; import java.io.Serial

我试图在它们自己的类中“marshall”和“unmarshall”对象,并使用“Marshaller”

主要方法:

public class Main {

    public static void main(String[] args) {
        new Something();        
    }
}
将生成要marshall或unmarshall的实例的“Something”类:

import java.io.File;
import java.io.Serializable;

public class Something implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public int value = 2;
    private File file = new File("something.serial");
    private static Marshaller marshaller = new Marshaller();

    Something() {

        value = 3;
        marshaller.marshalling(file, this);
        Something anotherThing = (Something) marshaller.unmarshalling(file);
        System.out.println(anotherThing.value);
    }
}
这是马歇尔:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Marshaller {

    public Marshaller() {

}

    /**
     * Charge l'objet sérializé d'un fichier si il existe
     * @param file : le fichier à désérialiser
     * @return obj : l'instance d'objet désérialisé
     */
    public Object unmarshalling(File file) {
        Object obj = null;
        ObjectInputStream ois;
        try {
            BufferedInputStream bis = new BufferedInputStream(
            new FileInputStream(file));
            ois = new ObjectInputStream(bis);
            obj = ois.readObject();
            ois.close();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        } catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
        return obj;
    }

    /**
     * Permet d'enregistrer un objet (settings, client...) dans un fichier
     * @param file : le fichier support de la sérialisation
     * @param obj : l'instance d'objet à sérialiser
     * 
     */
    public void marshalling(File file, Object obj) {
        ObjectOutputStream oos;
        try {
            oos = new ObjectOutputStream(
            new BufferedOutputStream(
            new FileOutputStream(file)));
            oos.writeObject(obj);
            oos.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
在类
Something
中,如果我的封送拆收器没有声明为“static”,我会得到一个
java.io.NotSerializableException
。为什么?

谢谢你的回答,帮助我理解

祝你今天愉快


PS:我应该使用单词
marshall
还是
serialize

当你序列化一个对象时,可以从该对象访问的所有对象都会被存储,所以它们都必须是可序列化的。如果您像这样声明您的类:

public class Something implements Serializable {
    private static final long serialVersionUID = 1L;
    public int value = 2;
    private File file = new File("something.serial");
    private Marshaller marshaller = new Marshaller();
}
然后,类
某物
的任何实例的可访问字段都是
文件
封送器
,因此jvm也会尝试对它们进行序列化,这要求它们中的每一个都必须是
可序列化的
,但类
封送器
不是,然后发生异常

当您将一个字段声明为
static
时,您将使其成为类的成员,而不是单个实例,因为序列化只关心实例的当前状态,只有与特定实例关联的数据才会被序列化,因此所有静态字段都将被忽略,程序正常工作

如果确实需要一个字段作为实例成员,但不希望对其进行序列化,则需要使用关键字
transient
声明该字段,然后在序列化过程中忽略该字段

对于单词
marshall
serialize
,我不是以英语为母语的人,所以我不能说太多它们之间的区别,但就我个人而言,在这种情况下我更喜欢
serialize
,在处理java对象和java对象之间的JSON/XML时使用
marshall


希望这对您有所帮助:-)

如果您的
封送员
位于
某物
的实例字段中,那么当
某物
被序列化时,它将尝试被序列化,因为它是
某物
实例的一部分。如果它是静态的,则它不是实例的一部分,因此不需要序列化。您可以尝试使用
transient
来阻止它序列化。请注意,您可以使
封送
解组
实用方法
静态
。是的,非常完美。非常感谢。