This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serial Version ID

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using JBoss Eclipse IDE 1.6

CODE :


at my class declation it says "The serializable class Controller does not declare a static final SerialVersionID field of type long"

What does that mean ?? what is the use/benifit of declaring serial version id ???
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Googling found this out.It is required for serialization.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, Eclipse encourages you to provide a serial UID for all serializable classes as this improves usage of serializable classes in the long run.

You can turn this warning off, or even make it an error if you like.

Moving to the IDE forum, not a servlet question.
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have just used the same code but i am not getting any errors? Is is somthing to do with jdk version?

Thanks,
PK
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not an error. It is only a suggestion to provide a UID when using a serializable class. I get it when using eclipse. The original post mentions JBoss also. Maybe you're IDE isn't generating it or you may have turned it off.
 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned by Anupam, it is a setting at Eclipse end as a warning when the code is compiled. If you have changed it to be flagged as an error, then Eclipse will report it as an error.

To ans your other question.. What serialVersionUID is.. Well it is a unique identifier for a class that can be serialized. All classes that can be serialized implement the marker interface java.io.Serializable. It is a mechanism using which JVM checks if the class had changed after it was serialized into a persistent medium. Example: I have UserBean class which had the following code:

------------------------------------
import java.io.Serializable;

public class UserBean implements Serializable
{
private int age = 0;

public int getAge()
{
return this.age;
}

public void setAge( int age )
{
this.age = age;
}
}
------------------------------------

serialVersionUID for the above class is 5799876628235941012L

Say you save an object of this class to a flat file.

Later you change the implementation of the class to:
------------------------------------
import java.io.Serializable;

public class UserBean implements Serializable
{
private int userAge = 0;

public int getUserAge()
{
return this.userAge;
}

public void setUserAge( int userAge )
{
this.userAge = userAge;
}
}

------------------------------------
Now the serialVersionUID for this class is -2676956332959004712L;

When you try to deserialize the older class that was persisted, JVM will now be able to know the object is now stale and no longer valid.

How we get serialVersionUID for any class?? JDK is shipped with serial version UID generator.

Type in "serialver -show" in your command prompt and provide the class name.

Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic