-1

I have started to work with sending data over sockets the past few weeks. I started by sending multiple strings with my data and then parsing the data into floats or what ever type i needed it for. but over time i noticed my client would start to slow down because its reading and converting multiple strings and using them while my server is only dishing them out. So i began sending Serializable objects which work and are great but i have some questions about them.

public class Packet implements Serializable{
    private static final long serialVersionUID = -7896044384334791233L;
    //data here
}

a class i would send looks something like this but with the data i want to send. Removing the serial VersionUID creates a error. What is the serialVersionUID and if it is the same in all the objects i am sending why is this one long necessary for my program to work?

Ryan
  • 1,972
  • 2
  • 23
  • 36
  • 1
    See [this](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it). And can you explain the error that appears? – TNT Aug 31 '15 at 01:24
  • Theres no error im asking for just a explanation on what role serial versions play with sending objects via socket – Ryan Aug 31 '15 at 01:26

1 Answers1

0

It's a way to say what the format of the data is. If you delete a field from your class, you should change the serialVersionUuid or consumers of your object may read it incorrectly.

Check out this answer.

Why generate long serialVersionUID instead of a simple 1L?

See this link to see incompatible changes, like deleting fields. http://docs.oracle.com/javase/6/docs/platform/serialization/spec/version.html

Also you can override the default method that Java used to serialize your object with writeObject and readObject. This could be useful if you know that compressing the object data (for example) would lead to less network traffic between components. Or maybe there is some custom way you want to send your data that is more optimal than the default Java way.

See this answer. Java Custom Serialization

Community
  • 1
  • 1
tom
  • 1,331
  • 1
  • 15
  • 28
  • If you add a new field to you class you should *not* change the `serialVersionUID`. You need to read the Versioning chapter of the Object Serialization Specification. – user207421 Aug 31 '15 at 01:35
  • Really? Maybe I should. Can you tell me why? What are the circumstances which you should? – tom Aug 31 '15 at 01:43
  • I found the spec and included a reference to it in my answer. – tom Aug 31 '15 at 01:54
  • You're still wrong. If you delete a field from your class, it can still be read by old code *unelss* you change the `serialVersionUID`. Try it. Treating this thing as a version number is an urban myth, promoted partly no doubt by its poorly chosen name. – user207421 Aug 31 '15 at 02:51
  • I know you marked it a duplicate, but would you mind very much editing my answer so it's correct? You've taken the time twice to tell me I'm wrong so it shouldn't take too much more of your time to correct my mistake. Thank you so much, I am just trying to be helpful just like everyone else. – tom Sep 01 '15 at 11:48