This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes few Qs on serialVersionUID Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "few Qs on serialVersionUID" Watch "few Qs on serialVersionUID" New topic
Author

few Qs on serialVersionUID

Naveen Hegde
Greenhorn

Joined: Nov 28, 2006
Posts: 17
Hi Ranches,

I am working on URLyBird. Using RMI. Can anybody clarify following doubts regarding generating serialVersionUid?
1. Is it mandatory to declare own serialVersionUid? or can we leave this job to compiler?
2. Can 2 serializable classes can have same serialVersionUID?
3. These IDs make sense only for objects which are transported over network right? (like only Remote objects..)

i couldn't find much info regd this in this forum. so starting a fresh topic. pl help.

Thanks in advance.
Naveen


Thanks
---------------------------------------
Naveen Hegde, SCJP5.0, SCJD...
---------------------------------------
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Naveen,

A lot of information about serialVersionUID can be found here. After reading this article you should be able to answer your own questions.

The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an InvalidClassException is thrown.

1. I would declare it myself.
2. of course (all my classes in my assignment had the same serialVersionUID: 1979, my year of birth)
3. Every class that implements Serializable interface should declare a serialVersionUID.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Naveen Hegde
Greenhorn

Joined: Nov 28, 2006
Posts: 17
Hi Roel,
Thanks a lot for your quick reply.
Yes. Things are much clear now. I too prefer to follow your technique.
But one need to know when to change the serialversionUid (as defined in specs). This makes it mandatory for junior programmers to understand the importance of this value whenever making changes to the class .
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Using this thread....
Anybody know if somebody lost points by only implements serialiVersionUID field in classes that will be transfer by network ? and let anothers without !
Regard


Fernando Franzini - Java Blog
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Grande Fernando!

Anybody know if somebody lost points by only implements serialiVersionUID field in classes that will be transfer by network ?


Well, if the class is going to be transfered by network, then it must implement java.io.Serializable, and that's the only case you need the serialVersionUID. If you don't explicitly create one, then the compiler will create one for you. This is related to the version of the class. Whenever I create a class that implements Serializable, I always declare this field.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi brow Roberto
Would you mind to tell me wich classes were in your assignment ?
(by the way, sunday we have a soccer meet....sp x timão lolllll)
Regards.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Fernando,

Also extending from JFrame or Exception for example will lead to defining a serialVersionUID. And like I said before: if my IDE told me to define a serialVersionUID, I did.

I have 2 classes which implement Serializable, just my transfer objects which are transferred (hence the name) across the network.

Kind regards,
Roel
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2231
    
    7

If you use the -Xlint:serial when compiling your source code, then it will tell you which classes needs or inherit serialVersionUID. Most Swing subclasses, serializable classes and exception classes need one.

I declared my own serialVersionUID.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

... extending from JFrame ... will lead to [a requirement for] defining a serialVersionUID.

I would question whether anyone needs to extend from JFrame for this assignment. Are you truly creating a new class that is a JFrame to be used by other classes, or would it be more true to say that your GUI has a JFrame?


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Hi Andrew,

You are correct: the "has a" relationship is more appropriate in this assignment than an "is a" relationship.

And as a small note/remark: the ServerWindow in Denny's DVD is also extending JFrame

Kind regards,
Roel
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81



I knew somebody would call me out on that.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: few Qs on serialVersionUID
 
Similar Threads
ArrayList problem ...
Is there any need to write SerialVersionId inside my HttpServlet?
Strange Serialization issue - serialVersionUID need on an interface!?!
hibernate + An exception occurred: org.hibernate.type.SerializationException
Eclipse Servlet Warning