— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet426/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   422   423   424   425   426   427   428   429   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

101101 
101101 
10101000010 
1010 10 0 
01010 1 
1010101 
10101010 
1001010101 
Dog.class
1
You write a Dog class
2
You serialize a Dog object 
using that class
Dog object 
class version ID
#343
D
o
g ob
j
e
c
t
Object is 
stamped with 
version #343
101101 
101101 
101000010 
1010 10 0 
01010 1 
100001 1010 
0 00110101 
1 0 1 10 10
Dog.class
3
You change the Dog class
class version ID
#728
4
You deserialize a Dog object 
using the changed class
D
o
g ob
j
e
c
t
Object is 
stamped with 
version #343
101101 
101101 
101000010 
1010 10 0 
01010 1 
100001 1010 
0 00110101 
1 0 1 10 10
Dog.class
class version is 
#728
5
Serailization fails!!
The JVM says, “you can’t 
teach an old Dog new code”.


serialization 
and
file I/O
you are here
4
461
Each time an object is serialized, the object (including 
every object in its graph) is ‘stamped’ with a version 
ID number for the object’s class. The ID is called 
the serialVersionUID, and it’s computed based on 
information about the class structure. As an object is 
being deserialized, if the class has changed since the 
object was serialized, the class could have a different 
serialVersionUID, and deserialization will fail! But you 
can control this.
If you think there is ANY possibility that 
your class might 
evolve, put a serial version 
ID in your class.
When Java tries to deserialize an object, it compares 
the serialized object’s serialVersionUID with that of the 
class the JVM is using for deserializing the object. For 
example, if a Dog instance was serialized with an ID of, 
say 23 (in reality a serialVersionUID is much longer), 
when the JVM deserializes the Dog object it will first 
compare the Dog object serialVersionUID with the 
Dog class serialVersionUID. If the two numbers don’t 
match, the JVM assumes the class is not compatible 
with the previously-serialized object, and you’ll get an 
exception during deserialization.
So, the solution is to put a serialVersionUID 
in your class, and then as the class evolves, the 
serialVersionUID will remain the same and the JVM 
will say, “OK, cool, the class is compatible with this 
serialized object.” even though the class has actually 
changed.
This works only if you’re careful with your class 
changes! In other words, you are taking responsibility 
for any issues that come up when an older object is 
brought back to life with a newer class. 
To get a serialVersionUID for a class, use the serialver 
tool that ships with your Java development kit.
Using the serialVersionUID
File Edit Window Help serialKiller

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   422   423   424   425   426   427   428   429   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish