为什么可序列化内部类不可序列化?

发布于 2024-12-01 03:36:45 字数 1061 浏览 3 评论 0原文

以下代码:

public class TestInnerClass {

    public static void main(String[] args) throws IOException {
        new TestInnerClass().serializeInnerClass();
    }

    private void serializeInnerClass() throws IOException {
        File file = new File("test");
        InnerClass inner = new InnerClass();
        new ObjectOutputStream(new FileOutputStream(file)).writeObject(inner);
    }

    private class InnerClass implements Serializable {

        private static final long serialVersionUID = 1L;

    }

}

抛出以下异常:

Exception in thread "main" java.io.NotSerializableException: TestInnerClass

我猜内部类有一个 TestInnerClass.this 字段,允许它私有访问 TestInnerClass 的字段和方法。将内部类声明为静态解决了这个问题 ,但是如果 InnerClass 需要此访问权限怎么办?有没有办法在没有封闭类的情况下序列化非静态内部类,例如通过引用外部类transient

编辑:例如,仅在序列化之前才需要访问外部类。好吧,编译器无法知道这一点,但我认为这就是 transient 关键字存在的原因。

The following code:

public class TestInnerClass {

    public static void main(String[] args) throws IOException {
        new TestInnerClass().serializeInnerClass();
    }

    private void serializeInnerClass() throws IOException {
        File file = new File("test");
        InnerClass inner = new InnerClass();
        new ObjectOutputStream(new FileOutputStream(file)).writeObject(inner);
    }

    private class InnerClass implements Serializable {

        private static final long serialVersionUID = 1L;

    }

}

throws the following exception:

Exception in thread "main" java.io.NotSerializableException: TestInnerClass

I guess the inner class has a TestInnerClass.this field that allows it private access to TestInnerClass's fields and methods. Declaring the inner class static solves it, but what if InnerClass needs this access? Is there a way to serialize a non-static inner class without the enclosing class, e.g. by making the reference to the outer class transient?

edit: for example, access to the outer class could be needed only before serialization. OK, the compiler cannot know that, but I thought that's why the transient keyword exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天 2024-12-08 03:36:45

如果 InnerClass 需要此访问权限怎么办?

然后它需要外部类实例,并且它必须与内部类一起序列化。

有没有办法在没有封闭类的情况下序列化非静态内部类,例如通过引用外部类瞬态?

不。当您反序列化这样一个类,然后尝试调用外部类的实例方法时,会发生什么情况?空指针异常?

what if InnerClass needs this access?

Then it needs the outer class instance, and it must be serialized along with the inner class.

Is there a way to serialize a non-static inner class without the enclosing class, e.g. by making the reference to the outer class transient?

No. What would happen when you deserialize such a class and then try to call an instance method of the outer class? A NullPointerException?

漆黑的白昼 2024-12-08 03:36:45

让 TestInnerClass 可序列化怎么样?

public class TestInnerClass implements Serializable { }

how about make TestInnerClass serializable?

public class TestInnerClass implements Serializable { }
烈酒灼喉 2024-12-08 03:36:45

InnerClass 无法序列化,因为要实例化它(根据反序列化期间的要求),您需要引用外部类的实例

。没有外部类的实例,内部类的实例就不能存在。

OuterClass o = new OuterClass();
OuterClass.InnerClass o_i = o.new InnerClass();

如果使用静态内部类,则可以序列化静态内部类。静态内部类的实例可以独立创建。

IE

OuterClass o = new OuterClass();
OuterClass.InnerClass i = new OuterClass.InnerClass();

InnerClass cannot be serialised because to instantiate it (as required during deserialization) you need a reference to an instance of the outer class

Instances of inner classes cannot exist without an instance of the outer class.

i.e

OuterClass o = new OuterClass();
OuterClass.InnerClass o_i = o.new InnerClass();

If you use static inner class, it will be possible to serialise the static inner class. The instances of static inner classes can be created standalone.

i.e

OuterClass o = new OuterClass();
OuterClass.InnerClass i = new OuterClass.InnerClass();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文