Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

few Qs on serialVersionUID

 
Naveen Hegde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Naveen Hegde
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
2
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fernando Franzini
Ranch Hand
Posts: 489
2
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I knew somebody would call me out on that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic