Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我不';不存在NotSerializableException吗?_Java_Serializable_Externalizable - Fatal编程技术网

Java 为什么我不';不存在NotSerializableException吗?

Java 为什么我不';不存在NotSerializableException吗?,java,serializable,externalizable,Java,Serializable,Externalizable,为什么我没有NotSerializableException原因是在序列化的类A中我有未序列化的privateb,我知道如果类实现Serializable所有组合类也必须实现Serializable/Externalizable import java.io.*; public class Test { public static void main(String[] args) throws IOException { FileOutputStream fil

为什么我没有
NotSerializableException
原因是在序列化的类
A
中我有未序列化的
privateb
,我知道如果类实现
Serializable
所有组合类也必须实现
Serializable
/
Externalizable

import java.io.*;

public class Test
{
    public static void main(String[] args) throws IOException
    {
        FileOutputStream fileOutput = new FileOutputStream("a.dat");
        ObjectOutputStream outputStream = new ObjectOutputStream(fileOutput);
        outputStream.writeObject(new A());
        fileOutput.close();
        outputStream.close();
    }
}

class A implements Serializable
{
    private int age;
    private B b;
}

class B
{

}

序列化
newa()
时,
b
null
所以没有
b

运行时类型很重要-例如,您可以让
类B驱动扩展B实现java.ui.Serializable
。或者可以从
列表
字段引用序列化
数组列表

如果使用非null、非可序列化类型初始化
b
,则可以看到异常。比如说

    private B b = new B();