简体   繁体   English

像这样的非静态内部类只能通过在反序列化时使用默认的无参数构造函数实例化错误

[英]Non-static inner classes like this can only by instantiated using default, no-argument constructor error when deserialize

I am trying to serialize and deserialize my object but I got an error during deserialization:我正在尝试序列化和反序列化我的对象,但在反序列化过程中出现错误:

Cannot construct an instance of 'MyObject' (although at least one Creator exists): 
non-static inner classes like this can only be instantiated using a default, no-argument constructor.

I had constructor for class MyObject with 2 parameters but I changed it to default constructor, but it still throw the same error.我有带有 2 个参数的类 MyObject 的构造函数,但我将它更改为默认构造函数,但它仍然抛出相同的错误。

This is my code:这是我的代码:

@Test
public void serializeAndDeserializeTest() throws JsonProcessingException {
    var list = new MyObject[2];
    var t1 = new MyObject();
    t1.value1 = TestEnum.VALUE5.numVal;
    t1.value2 = "A";
    var t2 = new MyObject();
    t2.value1 = TestEnum.VALUE1.numVal;
    t2.value2 = "B";
    list[0] = t1;
    list[1] = t2;
    var mapper = new ObjectMapper();
    var json = mapper.writeValueAsString(list);
    MyObject[] output = mapper.readValue(json, MyObject[].class);
}

public class MyObject
{
    public int value1;
    public String value2;
}

public enum TestEnum
{
    VALUE1(1),
    VALUE3(3),
    VALUE5(5);

    public int numVal;

    TestEnum(int numVal) {
        this.numVal = numVal;
    }
}

I also tried to create a private default constructor but it doesn't work.我还尝试创建一个私有默认构造函数,但它不起作用。

In order to fix this, make the MyObject inner class static:为了解决这个问题,将MyObject内部类设为静态:

public static class MyObject {
    public int value1;
    public String value2;
}

Without the static keyword, the inner class needs an instance of the encompassing outer class in order to exist.如果没有 static 关键字,内部类需要包含外部类的实例才能存在。 This is what causes the exception.这就是导致异常的原因。

For the enum that is not the case because inner enums are implicitly static, see the Java Language Specification, chapter 8.9对于不是这种情况的enum ,因为内部枚举是隐式静态的,请参阅Java Language Specification,第 8.9 章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我只有在拥有内部类时才能获得“无法从静态上下文引用的非静态变量”错误? - Why do I get the “non-static variable this cannot be referenced from a static context” error only when I have inner classes? 非静态内部类中的静态变量 - Static variables in non-static inner classes Java外部和内部类的方法名称相同:非静态方法静态上下文错误 - Java outer and inner classes have method with the same name: non-static method static context error 错误:“非静态Java函数'evaluate'的第一个参数不是有效的对象引用。' 使用TrasformFactory时 - ERROR: 'The first argument to the non-static Java function 'evaluate' is not a valid object reference.' when using TrasformFactory 在非静态内部类中使用泛型 - Using generics in non-static inner class (非静态)内部类的构造方法引用? - Constructor method reference for (non-static) inner class? 为什么在Java中定义内部(非静态嵌套)类时使用访问说明符? - Why use access specifiers when defining inner (non-static nested) classes in Java? 如何实例化多个嵌套的非静态内部类-Java - How to instantiate multiple nested non-static inner classes - java Java中的非静态内部类和序列化有什么问题 - What are the issues with non-static inner classes and serialization in Java Java,序列化非静态内部类。 有把戏吗? - Java, serializing non-static inner classes. Is there a trick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM