Results 1 to 8 of 8

Thread: Serialization and serialVersionUID in Java

  1. #1
    Join Date
    Dec 2008
    Posts
    202

    Serialization and serialVersionUID in Java

    I recently had with a colleague, a very controversial discussion on "Serialization and serialVersionUID. And I started the discussion with the following designs and ideas put forward.

    If a Java class can implement the interface Serializable and does not define any static class variable called serialVersionUID, you get the following warning from Eclipse:
    The serializable class XYZ Does not declare a static final serialVersionUID field of type long.
    I want you now to give time for reflection, whether this mechanical response is really useful. First, one must be clear about what you need serialization at all and what is the meaning of these serialVersionUID actually. This is, among other things, the case when it comes to transfer Java objects in binary form based on a network protocol (eg RMI or Spring HTTP invoker) between a client and an Application Server. If I want to send an instance of the class XYZ from client to server, the class must be serializable and XYZ be known on both the client and the server.

    Here it can happen in principle, that is present on the client a different version of the class as on the server, so an inconsistency between the client and server exist, which can easily lead to a malfunction of the application. To detect such inconsistencies, but it is exactly the serialVersionUID mechanism. Constitutes the JRE is deserialized on a fixed network received instance, the serialVersionUID of this instance does not match the serialVersionUID of the loaded class, it throws an exception, so the inconsistency detected quickly and (for example, by redeployment of the application) can be adjusted.

    Anyone having different opinions feel free to post here.

  2. #2
    Join Date
    Dec 2008
    Posts
    161

    Re: Serialization and serialVersionUID in Java

    What actually happens when it is specified in a serializable class has no explicit serialVersionUID? Then is given implicitly by the JRE when loading a class. And while this is done through a complicated calculation, included in the all class attributes and the signatures of all non-private methods. This means that once you add a class attribute, a delete, change the type or something changes in the interface automatically changes implicit serialVersionUID. Changing only the implementation of a method, this has no effect on the calculated implicit serialVersionUID!

  3. #3
    Join Date
    Jan 2006
    Posts
    211

    Re: Serialization and serialVersionUID in Java

    If I decide to award an explicit serialVersionUID, so I'm in the duty to maintain this in the future! So that for each class change, which I do, I need to think about whether I need to change the serialVersionUID. In my experience, does so almost no developer and I have even this little pleasure. But if the serialVersionUID is not maintained, the whole mechanism is bypassed and useless. Because of these considerations, I usually do without an explicit serialVersionUID to serializable classes instead and add more annotation.

  4. #4
    Join Date
    Jan 2009
    Posts
    143

    Re: Serialization and serialVersionUID in Java

    It sounds interesting and plausible. What happens if you can generate from Eclipse any UID, but the default UID used?
    • private static final long serialVersionUID = 1L;
    Is it the same way then, as if a generated id with the class version does not change, or more so that when loading the class a new UID is generated. If the latter is the case, you could be generated in the Eclipse always DefaultId.

  5. #5
    Join Date
    Feb 2009
    Posts
    105

    Re: Serialization and serialVersionUID in Java

    Is it the same way then, as if a generated id with the class version does not change, or more so that when loading the class a new UID is generated. If the latter is the case, you could be generated in the Eclipse always DefaultId.
    What value for serialVersionUID is set, not interested in the VM. As soon as one is set, does not generate internally. Thus, one must again take care of itself to the consistency of versions etc.. It therefore makes no difference whether the value to 1 or is a value generated by Eclipse.

  6. #6
    Join Date
    Jul 2006
    Posts
    442

    Re: Serialization and serialVersionUID in Java

    My arguments have yet a small mistake by the way: the calculation of the implicit serialVersionUID does not happen when loading the class but the first time you serialize / deserialize an instance. But that is irrelevant because it happened more than once after loading the class. There used to time the argument that the calculation cost of the implicit serialVersionUID time. But this is irrelevant, because the few milliseconds, the price of this one-time calculation, in a typical enterprise application completely irrelevant.

  7. #7
    Join Date
    Jul 2006
    Posts
    286

    Re: Serialization and serialVersionUID in Java

    I personally almost never produce an explicit serialVersionUID, so strike just such inconsistencies. The only exception: if the serialized data is not transient (eg, temporarily stored), I forgive a UID, so I at least have the chance to deserialize the data, if that took place during the storage of a deployment. I effective Java just not at hand will put the recommendation on a UID there always justified in any way?

  8. #8
    Join Date
    Aug 2006
    Posts
    235

    Re: Serialization and serialVersionUID in Java

    The grounds of Bloch, is that the calculation of serialVersionUID extremely sensitive response to changes in the class, ie even a small change (eg in a method or class variable) that a different value comes out. When I enter a client / server scenario but does not allow that there are different levels on client and server of a serializable class (because this would entail a considerable additional complexity, I would have to dominate), this sensitivity to change but just what I want: an automatic mechanism reveals the different levels on client and server side. A very different scenario is the long-term storage of serialized instances on record. It uses a sophisticated versioning approach is needed, since I at a later date must be able to read again what I have written out at some times. In such a scenario, an explicit serialVersionID it will become very important.

Similar Threads

  1. Binary Serialization in Java
    By blueprats in forum Guides & Tutorials
    Replies: 2
    Last Post: 30-04-2010, 04:22 PM
  2. Serialization in Vb.net
    By samualres in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 11:23 AM
  3. Advantages of serialization
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 06-02-2010, 11:51 AM
  4. What does serialization mean in java?
    By Honorata in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 08:35 AM
  5. Some questions regarding serialization
    By MABON in forum Software Development
    Replies: 3
    Last Post: 13-11-2009, 04:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,278,122.66818 seconds with 17 queries