java中final的sessionID是干嘛用的?

boooch 2011-09-29 09:31:59
经常见别人写的代码中会有一个final的sessionID,一串特别长的数字,为什么要这么写呢,这是做啥用的?这串数字是怎么生成的?
...全文
78 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shine333 2011-09-29
  • 打赏
  • 举报
回复
最好定义一个,因为你不定义,他也会帮你根据class文件算一个
shine333 2011-09-29
  • 打赏
  • 举报
回复
不是sessionId,是serialVersionUID。
简单说就是为了检查反序列化的时候运行的class版本和序列化的时候是否一致。
假设
public class A implements Serializable {int x;}

你把它序列化到了一个文件里面,若干天后,你想重新读出来,这个时候,你的版本已经改为了
public class A implements Serializable {int x;String y;}

这个时候两个版本就不一致了,当然这个例子,并不会导致很严重的问题,只不过y=null而已。

但是如果你不想要这个效果,或者不允许有版本不一致,那么就:
public class A implements Serializable {
private static final long serialVersionUID = 1L; // 或者其他随机数字
int x;}

public class A implements Serializable {
private static final long serialVersionUID = 555L; // 或者其他不同数字
int x;String y;
}

这样就会检查出版本不一致

/**
* The serialization runtime associates with each serializable class a version
* number, called a serialVersionUID, which is used during deserialization to
* verify that the sender and receiver of a serialized object have loaded
* classes for that object that are compatible with respect to serialization.
* If the receiver has loaded a class for the object that has a different
* serialVersionUID than that of the corresponding sender's class, then
* deserialization will result in an {@link InvalidClassException}. A
* serializable class can declare its own serialVersionUID explicitly by
* declaring a field named <code>"serialVersionUID"</code> that must be static,
* final, and of type <code>long</code>:<p>
*
* <PRE>
* ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
* </PRE>
*
* If a serializable class does not explicitly declare a serialVersionUID, then
* the serialization runtime will calculate a default serialVersionUID value
* for that class based on various aspects of the class, as described in the
* Java(TM) Object Serialization Specification. However, it is <em>strongly
* recommended</em> that all serializable classes explicitly declare
* serialVersionUID values, since the default serialVersionUID computation is
* highly sensitive to class details that may vary depending on compiler
* implementations, and can thus result in unexpected
* <code>InvalidClassException</code>s during deserialization. Therefore, to
* guarantee a consistent serialVersionUID value across different java compiler
* implementations, a serializable class must declare an explicit
* serialVersionUID value. It is also strongly advised that explicit
* serialVersionUID declarations use the <code>private</code> modifier where
* possible, since such declarations apply only to the immediately declaring
* class--serialVersionUID fields are not useful as inherited members. Array
* classes cannot declare an explicit serialVersionUID, so they always have
* the default computed value, but the requirement for matching
* serialVersionUID values is waived for array classes.
*/
Rainness_Zhang 2011-09-29
  • 打赏
  • 举报
回复
新手,同求

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧