<h2 class="rich_media_title" id="activity-name">
講真,下次打死我也不敢隨便改serialVersionUID了
</h2>
<div id="meta_content" class="rich_media_meta_list">
<span class="rich_media_meta rich_media_meta_nickname" id="profileBt">
<a id="js_name">
碼農沉思錄 </a>
<div id="js_profile_qrcode" class="profile_container" style="display:none;">
<div class="profile_inner">
<strong class="profile_nickname">碼農沉思錄</strong>
<img class="profile_avatar" id="js_profile_qrcode_img" src="" alt="">
<p class="profile_meta">
<label class="profile_meta_label">微信號</label>
<span class="profile_meta_value">code-thinker</span>
</p>
<p class="profile_meta">
<label class="profile_meta_label">功能介紹</label>
<span class="profile_meta_value">筆者為國內某知名企業不知名碼農,專注Java Web領域多年,有豐富的bug開發經驗。</span>
</p>
</div>
<span class="profile_arrow_wrp" id="js_profile_arrow_wrp">
<i class="profile_arrow arrow_out"></i>
<i class="profile_arrow arrow_in"></i>
</span>
</div>
</span>
<em id="publish_time" class="rich_media_meta rich_media_meta_text">3天前</em>
</div>
<div class="rich_media_content " id="js_content">
<p style="font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;background-color: rgb(255, 255, 255);text-align: right;" data-mpa-powered-by="yiban.io"><span style="color: rgba(0, 0, 0, 0.65);font-family: 'Monospaced Number', 'Chinese Quote', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;text-align: start;white-space: pre;letter-spacing: 1.4px;font-size: 12px;"></span></p><p style="text-align: center;"><img class="rich_pages " data-ratio="0.666" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/XA3sPCPib1l5P34dAsQQPqibib8tyCUCMnwX6RXYICMHYibO8b56dpM8cuHhew6LceId2PJiaEbdsKDZ4Eggzur5UyA/640?wx_fmt=jpeg" data-type="jpeg" data-w="500" style="width: 500px !important; height: auto !important; visibility: visible !important;" _width="500px" src="" crossorigin="anonymous" data-fail="0"></p><section class="code-snippet__fix code-snippet__js"><pre class="code-snippet__js" data-lang="javascript"><code class="hljs"><span class="code-snippet_outer">來源:Java成長路</span></code><code class="hljs less"><span class="code-snippet_outer">鏈接:<span class="hljs-attribute"><span class="hljs-attribute">https</span></span>:<span class="code-snippet__comment"><span class="hljs-comment"><span class="hljs-comment">//www.jianshu.com/p/a4508a8f2420</span></span></span></span></code></pre></section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;">序列化是一種對象持久化的手段。普遍應用在網絡傳輸、RMI等場景中。類通過實現 java.io.Serializable 接口以啟用其序列化功能。</section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;">但是,還有一個知識點並未展開介紹,那就是關於serialVersionUID 。這個字段到底有什么用?如果不設置會怎么樣?為什么《阿里巴巴Java開發手冊》中有以下規定:</section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;"><img class="" data-ratio="0.1527777777777778" data-type="other" data-w="1080" data-src="https://mmbiz.qpic.cn/mmbiz/CvQa8Yf8vq11tWicgbQ0jTiaGle6Dibibo4ZXozI7y0sxrnbthHA5eZ8qRHia8kegttJeOTzWuEAttacty5mBibQHvuw/640?wx_fmt=other" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190909143819446-485477025.png" crossorigin="anonymous" data-fail="0"></section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;"><strong style="color: rgb(191, 54, 12);">背景知識</strong></section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;">在展開本文的介紹之前,先來簡單介紹一些和序列化有關的知識,</section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;"><strong style="color: rgb(191, 54, 12);">Serializable 和 Externalizable</strong></section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;">Java類通過實現 java.io.Serializable 接口以啟用其序列化功能。<strong style="color: rgb(191, 54, 12);">未實現此接口的類將無法進行序列化或反序列化。</strong>可序列化類的所有子類型本身都是可序列化的。</section><p style="color: rgb(62, 62, 62);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;white-space: normal;widows: 1;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;"></p><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;">如果讀者看過Serializable的源碼,就會發現,他只是一個空的接口,里面什么東西都沒有。<strong style="color: rgb(191, 54, 12);">Serializable接口沒有方法或字段,僅用於標識可序列化的語義。</strong>但是,如果一個類沒有實現這個接口,想要被序列化的話,就會拋出java.io.NotSerializableException異常。</section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;">它是怎么保證只有實現了該接口的方法才能進行序列化與反序列化的呢?</section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;">原因是在執行序列化的過程中,會執行到以下代碼:</section><section style="font-size: 14px;letter-spacing: 1.4px;white-space: normal;line-height: 2em;margin: 1.5em 5px !important;"><img class="" data-ratio="0.5575485799701047" data-type="other" data-w="669" data-src="https://mmbiz.qpic.cn/mmbiz/CvQa8Yf8vq11tWicgbQ0jTiaGle6Dibibo4Zr7iaP8yEwHzfzaicHTBfibGaF3BZbCKiboDanJqzLzuKLxUTeBbcPI8klA/640?wx_fmt=other" _width="669px" src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190909145454097-584363508.png
" crossorigin="anonymous" data-fail="0">




java.io.InvalidClassException: com.hollis.User1; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2


java.io.InvalidClassException: com.hollis.User1; local class incompatible: stream classdesc serialVersionUID = -2986778152837257883, local class serialVersionUID = 7961728318907695402
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = xxxxL;






<div class="read-more__area" id="js_more_read_area" style="display:none;">
</div>
<div class="reward_area tc reward_area_primary" id="js_preview_reward_author" style="display:none;">
</div>