![]() ![]() A relationship is a reference from one object to another. In Java, relationships are defined through object references (pointers) from a source object to the target. What is the use of serial. Version. UIDHere I will discuss the importance of the variable serial. Version. UID which are used in Serializable classes. Below is an example that will make you understand the exact use of the variable. Example Code. Employee. Class declarations define new reference types and describe how they are implemented (§8.1). A top level class is a class that is not a nested class. A nested class. Serialization in java is used to flatten the object and persist the flattened object or send to other JVMs. Answer: Serialization interface does not have any methods. It is a marker interface. It just tells that your class can be serializable. ![]() Serializable. public class Employee implements Serializable. String first. Name. String last. Name. Version. UID = 5. Serializaiton. Class. This class will be used to serialize). File. Output. Stream. IOException. import java. Object. Output. Stream. Serializaiton. Class {. String[] args) {. Employee emp = new Employee(). Name = "Vivekanand". Name = "Gautam". File. Output. Stream file. Out = new File. Output. Stream("./employee. Object. Output. Stream out = new Object. Output. Stream(file. Out). out. write. Object(emp). out. Out. close(). System. Serialized data is saved in ./employee. IOException i) {. Stack. Trace(). Deserialization. Class. java (This class will be used to deserialize ). Deserialization. Class {. String[] args) {. Employee emp = null. File. Input. Stream file. In = new File. Input. Stream("./employee. Object. Input. Stream in = new Object. Input. Stream(file. In). emp = (Employee) in. Object(). file. In. IOException i) {. Stack. Trace(). } catch (Class. Not. Found. Exception c) {. System. out. println("Employee class not found"). Stack. Trace(). System. Deserializing Employee.."). System. out. println("First Name of Employee: " + emp. Name). System. out. Last Name of Employee: " + emp. Name). Now execute “Serialization. Class. java” and then “Deserialization. Class. java”. It will first create a file with Employee object’s state and then while de- serialization it creates object from the same file. Output will be something like below. Serialized data is saved in ./employee. Deserializing Employee.. First Name of Employee: Vivekanand. Last Name of Employee: Gautam. Now let’s try and remove “serial. Version. UID” variable from Employee. Serialization. Class. It will create “employee. Now let’s add a new variable in Employee class suppose String Address. . Employee. java. package com. Serializable. public class Employee implements Serializable. String first. Name. String last. Name. String Address. //Variable is commented. Version. UID = 5. Now run “Deserialization. Class. java” and see the output. Booom. java. io. Invalid. Class. Exception: com. Employee; local class incompatible: stream classdesc serial. Version. UID = 5. Version. UID = - 3. Object. Stream. Class. Non. Proxy(Unknown Source). Object. Input. Stream. Non. Proxy. Desc(Unknown Source). Object. Input. Stream. Class. Desc(Unknown Source). Object. Input. Stream. Ordinary. Object(Unknown Source). Object. Input. Stream. Object. 0(Unknown Source). Object. Input. Stream. Object(Unknown Source). Deserialization. Class. Deserialization. Class. It will throw an incompatible exception. Because the given class Employee. Hence the system failed to identify that it is still the same class. To make our system understand that it is the same class you have to make use of serial. Version. UID variable inside class. You can try follow the above steps but keep serial. Version. UID intact and see the output. De- serialization process will work without any issue. Bullet Points. Defining a serial. Version. UID field in serializable class is not mandatory. If a serializable class has an explicit serial. Version. UID then this field should be of type long and must be static and final. If there is no serial. Version. UID field defined explicitly then serialization runtime will calculate default value for that class. The value can vary based on compiler implementation. Hence it is advisable to define serial. Version. UID. It is advised to use private access modifier for serial. Version. UID. Different class can have same serial.Version. UID. Array classes cannot declare an explicit serial. . Version. UID, so they always have the default computed value, but the requirement for matching serial.Version. UID values is waived for array classes.If there is a difference between serial.Version. UID of loaded reciever class and corresponding sender class then Invalid. Class. Exception will be thrown. You should use different serial. Version. UID for different version of same class if you want to forbid serialization of new class with old version of same class. If you do not provide serial. Version. Id in a class which is supposed to be serialised then compiler will give warning messages about the same. If you want to override this warning you can use given annotation. Once used, compiler will stop complaining about the missing serial. Version. UID. Series Navigation< < Transient vs Static variable java. Java Thread Tutorial > >.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2017
Categories |