Tuesday, 18 June 2019

Serialization | Generate serialVersionUID using Java Program


serialVersionUID can be generated by using getSerialVersionUID() method of the ObjectStreamClass class.

package com.algorithforum.serialization;

import java.io.ObjectStreamClass;
import java.io.Serializable;

class GenerateSerialVerUID implements Serializable {
    
     //private static final long serialVersionUID = 1L;

     public static void main(String[] args) {

          ObjectStreamClass osc = ObjectStreamClass.lookup(GenerateSerialVerUID.class);
          long serialID = osc.getSerialVersionUID();

          System.out.println(serialID);
     }
}

Output:
195513872176855581

Above the code will generate the same serialVersionUID even after running the program multiple times. If we will uncomment the predefined serialVersionUID, it will the value mentioned in the above program i.e. 1.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...