Get the SUID of a class at runtime

Invoke the method getSerialVersionUID on an instance of the class ObjectStreamClass.

Main.java:

import java.io.*;
 
public class Main implements Serializable {
   public static void main(String args[]) {
      ObjectStreamClass osc = ObjectStreamClass.lookup(Test1.class);
      System.out.println("serialVersionUID for class Test1: " + osc.getSerialVersionUID());
 
      osc = ObjectStreamClass.lookup(Test2.class);
      System.out.println("serialVersionUID for class Test2: " + osc.getSerialVersionUID());
   }
}
 
class Test1 implements Serializable {
}
 
class Test2 implements Serializable {
   static final long serialVersionUID = 1;
}

outputs:

serialVersionUID for class Test1: 3264391750135642662
serialVersionUID for class Test1: 1