Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview


Question 57: What is SerialVersionUID?



Download 7,31 Mb.
Pdf ko'rish
bet35/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   31   32   33   34   35   36   37   38   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Question 57: What is SerialVersionUID?
Answer: SerialVersionUID : The serialization process at runtime
associates an id with each Serializable class which is known as
SerialVersionUID. It is used to verify the sender and receiver of the
serialized object. The sender and receiver must have the same
SerialVersionUID, otherwise, InvalidClassException will be thrown
when you deserialize the object. A Serializable class can declare its
own UID explicitly by declaring a field. It must be static, final and of
type long. Remember, there is an exception for SerialVersionUID
that although it is static, it gets serialized too, so that at the object
deserialization the sender and receiver can be verified.
If a serializable class doesn’t explicitly declare a serialVersionUID,
then the serialization runtime will calculate a default one for that
class based on various aspects of class. This default
serialVersionUID gets changed, when you add a new field or remove
the transient keyword from a variable or convert the static variable to
non-static variable. And if you are modifying the class structure after
Serialization has been done, you will not be able to deserialize the
object, see the example below:
Let’s change the Employee class in our previous example and
remove the transient keyword from salary variable:
Program 1:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
57\Program 1\color1.png


We have already serialized the Employee object in our previous
example, now let’s try to de-serialize it back:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
57\Program 1\color2.png


Output:
java.io.InvalidClassException
: com.serialization.demo.Employee;
local class incompatible: stream classdesc serialVersionUID =
-3697389390179909057, local class serialVersionUID =
-3759917827722067163
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown
Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.serialization.demo.DeserializationTest.main(
DeserializationTest.java:13
)
Hence, it is strongly recommended that all serializable classes
explicitly declare serialVersionUID value, in case you are using an
IDE and not giving any serialVersionUID, compiler will give you a
warning like below:




Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   31   32   33   34   35   36   37   38   ...   89




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