Home Java javaTutorial What are common Java serialization errors?

What are common Java serialization errors?

Apr 16, 2024 pm 06:18 PM
java access overflow serialization error

Common Java serialization errors include: Class version conflict (InvalidClassException) No serializable superclass or interface declared (NotSerializableException) Access denied or illegal reflection serialized object (IllegalAccessException) Serialization of static fields is variable Or circular reference (StackOverflowException or inconsistent state)

What are common Java serialization errors?

Common Java serialization errors

Java serialization errors : An error that occurs when converting an object to or reconstructing an object from a binary stream. It is usually caused by the following reasons:

1. Class version conflict

  • The object that needs to be serialized must be compatible with the class version when the object is reconstructed. If incompatible, an InvalidClassException error is thrown.
class MyClass implements Serializable {
    private static final long serialVersionUID = 1L;

    private String name;

    // 省略其他代码...
}

// 序列化对象后修改了 MyClass
MyClass myObject = new MyClass();
myObject.setName("John Doe");

// 将对象序列化到文件
FileOutputStream fos = new FileOutputStream("object.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(myObject);
oos.close();

// 更改 MyClass 的 serialVersionUID
MyClass.serialVersionUID = 2L;

// 从文件中读取并反序列化对象
FileInputStream fis = new FileInputStream("object.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
MyClass deserializedObject = (MyClass) ois.readObject();
ois.close();
Copy after login

2. No serializable superclass or interface declared

  • Any serializable subclass must declare its direct super Classes or implemented interfaces are also serializable. Otherwise, it causes NotSerializableException.
class NotSerializable {
    // ...
}

class MyClass extends NotSerializable implements Serializable {
    // ...
}
Copy after login

3. Access Denied or Illegal Reflection

  • Serialized objects must have access modifiers with private The writeObject and readObject methods. Reflective access to these methods results in IllegalAccessException.
class MyClass implements Serializable {
    private void writeObject(ObjectOutputStream oos) throws IOException {
        // ...
    }

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        // ...
    }
}

// 使用反射调用 writeObject
ObjectOutputStream oos = new ObjectOutputStream(new ByteArrayOutputStream());
oos.writeObject(myObject);
Method m = MyClass.class.getDeclaredMethod("writeObject", ObjectOutputStream.class);
m.setAccessible(true);
m.invoke(myObject, oos);
Copy after login

4. Serialization of static fields

  • Static fields will not be serialized. If you want to serialize them, declare them as transient (transient).
class MyClass implements Serializable {
    private static String staticField;
    
    private String instanceField;

    // ...
}
Copy after login

5. Mutable or circular references

  • Circular references will cause StackOverflowException. Mutable objects can lead to inconsistent state.
// 可变对象
class MyClass implements Serializable {
    private int mutableField;
    
    // ...
}

// 循环引用
class MyClass1 implements Serializable {
    private MyClass myClass2;

    class MyClass2 implements Serializable {
        private MyClass1 myClass1;
    }
}
Copy after login

The above is the detailed content of What are common Java serialization errors?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The price of Bitcoin since its birth 2009-2025 The most complete summary of BTC historical prices The price of Bitcoin since its birth 2009-2025 The most complete summary of BTC historical prices Jan 15, 2025 pm 08:11 PM

The price of Bitcoin since its birth 2009-2025 The most complete summary of BTC historical prices

How to convert deepseek pdf How to convert deepseek pdf Feb 19, 2025 pm 05:24 PM

How to convert deepseek pdf

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

A list of historical prices since the birth of Bitcoin BTC historical price trend chart (Latest summary) A list of historical prices since the birth of Bitcoin BTC historical price trend chart (Latest summary) Feb 11, 2025 pm 11:36 PM

A list of historical prices since the birth of Bitcoin BTC historical price trend chart (Latest summary)

Overview of the historical price of Bitcoin since its birth. Complete collection of historical price trends of Bitcoin. Overview of the historical price of Bitcoin since its birth. Complete collection of historical price trends of Bitcoin. Jan 15, 2025 pm 08:14 PM

Overview of the historical price of Bitcoin since its birth. Complete collection of historical price trends of Bitcoin.

Java Made Simple: A Beginner's Guide to Programming Power Java Made Simple: A Beginner's Guide to Programming Power Oct 11, 2024 pm 06:30 PM

Java Made Simple: A Beginner's Guide to Programming Power

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Create the Future: Java Programming for Absolute Beginners

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Java Program to Find the Volume of Capsule

See all articles