aspose file tools
The moose likes Java in General and the fly likes Why does serialization require a serial version UID in the subclasses of the implementing class. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why does serialization require a serial version UID in the subclasses of the implementing class." Watch "Why does serialization require a serial version UID in the subclasses of the implementing class." New topic
Author

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

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

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 .
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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 http://www.coderanch.com/t/527879/java-programmer-SCJP/certification/Deal-Changing-Objectives
For more details.
I hope this helps.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 276


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.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 341
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.


http://muhammadkhojaye.blogspot.com/
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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>
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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.

SCJP 6. Learning more now.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8712
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


Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why does serialization require a serial version UID in the subclasses of the implementing class.
 
Similar Threads
Serialization
SessionBean method returning POJO == exception?
Clustered Server - Session Handling
inheriting interfaces
Marker Interface