Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Please explain public vs private vs protected serialversionUID.Since we have to make unique serialversionUID why it is not private by default
- Mufim
If the class doesn’t define serialVersionUID, it’s getting calculated automatically by java compiler and assigned to the class as a private variable.
- Shubham Srivastava
Really nice one. Thanks a lot. Can you explain me oops concept with example.
- Sharanabasava
Hi Pankaj, it has always been great learning experience reading your blog posts. But of late an aspect of Serialization has been troubling me which led me to this post. However the blog content with regards to serializing a class instance with inheritance hierarchy is not consistent with the actual result that we get. You say that “Sometimes we need to extend a class that doesn’t implement Serializable interface. If we rely on the automatic serialization behavior and the superclass has some state, then they will not be converted to stream and hence not retrieved later on.” But I run the following code and it does serializes and deserializes the non serializable super type quite ok!! That is without any extra effort of implementing any special method - writeObject (…), readObject (…) in the serializable sub class. public class SerializingSuperClassDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { SubClass sub = new SubClass (); ObjectOutputStream oos = StreamUtility.returnOOS("C:\CoreCode\SuperSerialize.mnk"); oos.writeObject(sub); oos.close(); ObjectInputStream ois = StreamUtility.returnreOIS("C:\CoreCode\SuperSerialize.mnk"); sub = (SubClass)ois.readObject(); System.out.println(sub); } } class SuperClass { int supInt = 1; } class SubClass extends SuperClass implements java.io.Serializable { int subInt = 0; @Override public String toString (){ return supInt+" "+subInt; } } And here is the StreamUtility class: public class StreamUtility { public static ObjectOutputStream returnOOS(String fileName){ ObjectOutputStream oos = null; try { FileOutputStream fos = new FileOutputStream (fileName); oos = new ObjectOutputStream (fos); } catch (FileNotFoundException ex) { Logger.getLogger(StreamUtility.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(StreamUtility.class.getName()).log(Level.SEVERE, null, ex); } return oos; } public static ObjectInputStream returnreOIS (String fileName){ ObjectInputStream ois = null; try { FileInputStream fis = new FileInputStream (fileName); ois = new ObjectInputStream (fis); } catch (FileNotFoundException ex) { Logger.getLogger(StreamUtility.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(StreamUtility.class.getName()).log(Level.SEVERE, null, ex); } return ois; } } And here is the output 1 0 Could you kindly explain how was 1 read and deserialized when non serializable super type fields aren’t serialized. I would anxiously wait to read your reply.
- Mohammad Nawazish Khan
I think, In the first paragraph you have used the word Synchronization instead of Serialization. “Java Synchronization process seems very easy…” Right?
- Kartik
Hi Pankaj Thanks a lot. All your blog posts are really nice. . Could you please clarify me as ObjectOutputStream and ObjectInputStream implement the Autocloseable interface, then why we explicitly closing these two streams in the code.
- Lalatendu