serialversionuid

【Java】serialVersionUID的作用

十年热恋 提交于 2019-12-06 20:19:46
简单来说,Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。(InvalidCastException) serialVersionUID有两种显示的生成方式: 一个是默认的1L,比如:private static final long serialVersionUID = 1L; 一个是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如: private static final long serialVersionUID = xxxxL; 当你一个类实现了Serializable接口,如果没有显示的定义serialVersionUID,Eclipse会提供这个 提示功能告诉你去定义 。在Eclipse中点击类中warning的图标一下,Eclipse就会 自动给定两种生成的方式。如果不想定义它,在Eclipse的设置中也 可以把它关掉的,设置如下: Window ==> Preferences ==> Java ==> Compiler ==> Error/Warnings ==> Potential

Understand The SerialVersionUID

≯℡__Kan透↙ 提交于 2019-12-06 20:19:06
If you have ever implemented Serializable interface, you must encounter this warning message The serializable class xxx does not declare a static final serialVersionUID field of type long intro. So…what is serialVersionUID? The serialVersionUID is used as a version control in a Serializable class. If you do not explicitly declare a serialVersionUID, JVM will do it for you automatically, based on various aspects of your Serializable class, as described in the Java(TM) Object Serialization Specification . 1. SerialVersionUID Example The above statement is a bit hard to understand at the

serialVersionUID作用

☆樱花仙子☆ 提交于 2019-12-06 20:18:56
Java 的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的 serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就 会出现序列化版本不一致的异常。 Eclipse中The serializable class XXXXXX does not declare a static final serialVersionUID field of type long出现这样的警告处理办法。 当采用程序的Add default Serial version ID修复时,Eclipse会加上:private static final long serialVersionUID = 1L; 当采用程序的Add generated Serial version ID修复时,Eclipse会加上:private static final long serialVersionUID = xxxxL; 其实这个问题出现的具体原因是和序列化中的这个serialVersionUID有关。 serialVersionUID 用来表明类的不同版本间的兼容性。有两种生成方式: 一个是默认的1L,比如:private static final

serialVersionUID 的作用

*爱你&永不变心* 提交于 2019-12-06 03:58:21
serialVersionUID 用来表明类的不同版本间的兼容性 简单来说,Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。 当实现java.io.Serializable接口的实体(类)没有显式地定义一个名为serialVersionUID,类型为long的变量时,Java序列化机制会根据编译的class自动生成一个serialVersionUID作序列化版本比较用,这种情况下,只有同一次编译生成的class才会生成相同的serialVersionUID 。 如果我们不希望通过编译来强制划分软件版本,即实现序列化接口的实体能够兼容先前版本,未作更改的类,就需要显式地定义一个名为serialVersionUID,类型为long的变量,不修改这个变量值的序列化实体都可以相互进行串行化和反串行化。 来源: oschina 链接: https://my.oschina.net/u/586716/blog/71521

serialized lambda and no serialVersionUID?

浪子不回头ぞ 提交于 2019-12-06 02:13:43
问题 I'm trying to learn how the serialization works with Java and its lastest version. I'm trying to serialize a lambda like this : Runnable r = (Runnable & Serializable)() -> {System.out.println("This is a test");}; But I notice that I have no warning about the absence of a serialVersionUID variable. Is it normal ? I know it will be generated at the runtime however it is strongly recommended to define it : https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html If a serializable

Why does the serialVersionUID field exist?

天涯浪子 提交于 2019-12-05 21:30:08
It has baffled me from the launch of the Serializable interface why I have to incorporate this field in all of my classes. I understand that this interface needs a unique identifier to mark the class but why cant they generate this at run-time. For instance they could generate it using an MD5 hash of the fully-qualified class name or a similar methodology used to handle duplicates in their rare occurrence (Which is, I'm sure, what eclipse does when asked to generate the id anyway). So what I'm asking (no this post isn't just a rant against the standard library) is exactly how the serialization

How to get rid of InvalidClassException SerialVersionUID?

 ̄綄美尐妖づ 提交于 2019-12-05 03:33:38
I had saved one java object in the Database and then after few days I changed my jre version. Now when i tried to read that same object I am getting following exception: Exception in thread "main" java.io.InvalidClassException: SerializeMe; local class incompatible: stream classdesc serialVersionUID = -6377573678240024862, local class serialVersionUID = -8204757486033751616 How can I get rid of this,how can I get the saved object? please help me. If you can affect source code of this class and JRE was only thing that changed, most likely you can still deserialize object that was serialized by

serialVersionUID field warning in eclipse

南楼画角 提交于 2019-12-05 00:59:00
I have just started on AWT and made a simple program in it, it works fine but it shows a warning message in eclipse which i don't understand: The serializable class TestGUI does not declare a static final serialVersionUID field of type long I know that the warning message is not related to AWT and there was no need to post my whole code but when i tried to make a SSCCE of the code the warning also disappeared. Since I don't know why this warning is generated i didn't knew which part to retain in my SSCCE. Hence the whole code! My code is: import java.awt.Frame; import java.awt.Graphics; import

About generated serialVersionUID in Eclipse

谁说胖子不能爱 提交于 2019-12-05 00:45:28
Is there any way to generate serialVersionUID in Eclipse serially? By serially I want to mean that if one serializable class has serialVersionUID = 1L, then when I generate serialVersionUID of another class this will be serialVersionUID = 2L. If I manually specify 1L, 2L, 3L and so on, will this can create any problem? Eclipse gave an option to choose "Add generated serial version ID", is this option safe to choose? No, it won't create any problem in any of your two circumstances: you can manually specify it incrementally you can let Eclipse assign them random generated values you can keep

serialized lambda and no serialVersionUID?

非 Y 不嫁゛ 提交于 2019-12-04 07:11:55
I'm trying to learn how the serialization works with Java and its lastest version. I'm trying to serialize a lambda like this : Runnable r = (Runnable & Serializable)() -> {System.out.println("This is a test");}; But I notice that I have no warning about the absence of a serialVersionUID variable. Is it normal ? I know it will be generated at the runtime however it is strongly recommended to define it : https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a