• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Why does serialization require a serial version UID in the subclasses of the implementing class.

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ,

i have a doubt , according to the serialization rules the subclass of the class implementing serializable interface is also serializable , then why does my subclass require me to give it a serialversionUID .
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ragi singh wrote:Hi all ,

i have a doubt , according to the serialization rules the subclass of the class implementing serializable interface is also serializable , then why does my subclass require me to give it a serialversionUID .



Hello Ragi, If you are preparing to take the SCJP/OCP exams, be informed that Serialiozation is NO longer in the exams. However read the contents of this link https://coderanch.com/t/527879/java-programmer-SCJP/certification/Deal-Changing-Objectives
For more details.
I hope this helps.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that it's allowed -- some might say even encouraged -- to learn stuff that isn't on the exam. If you're very careful to only learn stuff that's on the exam, then frankly, you'll be ill-equipped for your first job.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Note that it's allowed -- some might say even encouraged -- to learn stuff that isn't on the exam. If you're very careful to only learn stuff that's on the exam, then frankly, you'll be ill-equipped for your first job.



Jackzia wrote: Hello Ernest, thank you for adding that, I was about to modify mine, it is VERY true, thats my 'Modus-Operandi'. However I thought it was a good idea that he is aware that serialization was NO longer there.

 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As per my knowledge SerialVersionUID if not provided is generated by compiler and it could be different if version of class got changed , so its best to provide that as constant in file itself.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ragi singh wrote:according to the serialization rules the subclass of the class implementing serializable interface is also serializable , then why does my subclass require me to give it a serialversionUID .


serialversionUID of a class is related to its member fields, class hierarchy, datatype etc. Therefore, the subclass will require its own versionUID.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The serialVersionUID is used to determine compatibility between the deseralized object and current version of the class.
and serialVersionUID is static , means *specific to a class*, not an objects of the class . so every class should contain serialVersionUID separately. more over, a two different class can have same serialVersionUID.

<edit>serialVersionUID is a serialization stream unique identifier</edit>
 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Note that it's allowed -- some might say even encouraged -- to learn stuff that isn't on the exam. If you're very careful to only learn stuff that's on the exam, then frankly, you'll be ill-equipped for your first job.



I had a intuition of that.
Can you suggest the additional things one should learn to be better prepared to face real world challenges ?

PS : I want well rounded knowledge first and then a cert , not the other way round.
 
author
Posts: 9000
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

This IS an interesting topic for sure.

And I totally agree that the OCPJP won't totally prepare you for the real world.

And... this is the OCPJP forum! So let's move this useful, real-world topic over to Java in General...

Bert
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so serialversionUID is for compatibility reasons.
But without that field deserialization will not succeed when there will be a diference in class.

From the documentation:
However, it is strongly recommended 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 InvalidClassExceptions 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 private 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.
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic