serialization in java.pdf
Post on 25-Jan-2016
24 Views
Preview:
TRANSCRIPT
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 1/13
Home » Java » Core Java » Serialization in java
ABOUT ARPIT MANDLIYA
I am java developer at Tata Consultancy Services Ltd. My current area of interest are J2EE,web development and java designpatterns. I am technology enthusiast trying to explore new technologies. In spare time,I love blogging.
Serialization in java Posted by: Arpit Mandliya in Core Java March 12th, 2013
Java provides mechanism called serialization to persists java objects in a form of ordered or sequence of bytes that includes the object’s dataas well as information about the object’s type and the types of data stored in the object. So if we have serialize any object then it can be readand deserialize it using object’s type and other information so we can retrieve original object.
Classes ObjectInputStream and ObjectOutputStream are highlevel streams that contain the methods for serializing and deserializing an object.ObjectOutputStream has many method for serializing object but commonly used method is:
Similarly ObjectInputStream has
Need of Serialization?Serialization is usually used When the need arises to send your data over network or stored in files. By data I mean objects and not text. Nowthe problem is your Network infrastructure and your Hard disk are hardware components that understand bits and bytes but not Java objects.Serialization is the translation of your Java object’s values/states to bytes to send it over network or save it.On other hand,Deserialization isconversion of byte code to corresponding java objects.
Concept of serialVersionUID :SerialVersionUID is used to ensure that same object(That was used during Serialization) is loaded during Deserialization.serialVersionUID isused for version control of object.You can read more at serialVersionUID in java serialization
For Serialization:Steps are :
NEWSLETTER
121704 insiders are already enjoyingweekly updates and complimentarywhitepapers!Join them now to gain access to the latest news in the Java world,as well as insights about Android, Scala,Groovy and other related technologies.
Email address:
Your email address
Sign up
JOIN US
With 1,240,600unique visitors and over500 authors we areplaced among the top Javarelated sites around.Constantly being on thelookout for partners; weencourage you to join us.So If you have a blog with
unique and interesting content then you shouldcheck out our JCG partners program. You can alsobe a guest writer for Java Code Geeks and honeyour writing skills!
CAREER OPPORTUNITIES
1 private void writeObject(ObjectOutputStream os) throws IOException2 3 4
1 private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException2 3 4
Find Jobs
what:title, keywords
where:city, state, or zip
ANDROID JAVA JVM LANGUAGES SOFTWARE DEVELOPMENT AGILE CAREER COMMUNICATIONS DEVOPS META JCG
Search... Knowledge Base Job Board Join Us About
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 2/13
Lets take an example: Create Employee.java in src>org.arpit.javapostsforlearning:
1.Employee.java
As you can see above,if you want to serialize any class then it must implement Serializable interface which is marker interface.
Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface. CreateSerializeMain.java in src>org.arpit.javapostsforlearning
2.SerializeMain.java
For Deserialization:Steps are:
01 package org.arpit.javapostsforlearning;02 import java.io.Serializable;03 public class Employee implements Serializable04 05 int employeeId;06 String employeeName;07 String department;08 09 public int getEmployeeId() 10 return employeeId;11 12 public void setEmployeeId(int employeeId) 13 this.employeeId = employeeId;14 15 public String getEmployeeName() 16 return employeeName;17 18 public void setEmployeeName(String employeeName) 19 this.employeeName = employeeName;20 21 public String getDepartment() 22 return department;23 24 public void setDepartment(String department) 25 this.department = department;26 27
01 package org.arpit.javapostsforlearning;02 import java.io.FileOutputStream;03 import java.io.IOException;04 import java.io.ObjectOutputStream;05 public class SerializeMain 06 07 /**08 * @author Arpit Mandliya09 */10 public static void main(String[] args) 11 12 Employee emp = new Employee();13 emp.setEmployeeId(101);14 emp.setEmployeeName('Arpit');15 emp.setDepartment('CS');16 try17 18 FileOutputStream fileOut = new FileOutputStream('employee.ser');19 ObjectOutputStream outStream = new ObjectOutputStream(fileOut);20 outStream.writeObject(emp);21 outStream.close();22 fileOut.close();23 catch(IOException i)24 25 i.printStackTrace();26 27 28
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 3/13
Create DeserializeMain.java in src>org.arpit.javapostsforlearning
3.DeserializeMain.java
4.Run it:First run SerializeMain.java then DeserializeMain.java and you will get following output:
So we have serialize an employee object and then deserialized it.It seems very simple but it can be very complex when referenceobject,inheritance come into the picture.So we will see different cases one by one and how we can apply serialization in different scenarios.
Case 1What if an object has a reference to other objectsWe have seen very simple case of serialization,now what if it also a reference to other objects.How will it serialized then? will reference objectwill also get serialized?.Yes,You don’t have to explicitly serialize reference objects.When you serialize any object and if it contain any otherobject reference then Java serialization serialize that object’s entire object graph.
For example:Lets say,Employee now has reference to address object and Address can have reference to some other object(e.g.Home) thenwhen you serialize Employee object all other reference objects such as address and home will be automatically serialized. Lets create Addressclass and add object of Address as a reference to above employee class.
Employee.java:
01 package org.arpit.javapostsforlearning;02 import java.io.IOException;03 import java.io.ObjectInputStream;04 05 public class DeserializeMain 06 /**07 * @author Arpit Mandliya08 */09 public static void main(String[] args) 10 11 Employee emp = null;12 try13 14 FileInputStream fileIn =new FileInputStream('employee.ser');15 ObjectInputStream in = new ObjectInputStream(fileIn);16 emp = (Employee) in.readObject();17 in.close();18 fileIn.close();19 catch(IOException i)20 21 i.printStackTrace();22 return;23 catch(ClassNotFoundException c)24 25 System.out.println('Employee class not found');26 c.printStackTrace();27 return;28 29 System.out.println('Deserialized Employee...');30 System.out.println('Emp id: ' + emp.getEmployeeId());31 System.out.println('Name: ' + emp.getEmployeeName());32 System.out.println('Department: ' + emp.getDepartment());33 34
1 Deserialized Employee...2 Emp id: 1013 Name: Arpit4 Department: CS
01 package org.arpit.javapostsforlearning;02 import java.io.Serializable;03 04 public class Employee implements Serializable05 06 int employeeId;07 String employeeName;08 String department;09 Address address;10 11 public int getEmployeeId() 12 return employeeId;
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 4/13
Create Address.java in org.arpit.javapostsforlearning:
Address.java:
Create SerializeDeserializeMain.java in org.arpit.javapostsforlearning:
SerializeDeserializeMain.java:
13 14 public void setEmployeeId(int employeeId) 15 this.employeeId = employeeId;16 17 public String getEmployeeName() 18 return employeeName;19 20 public void setEmployeeName(String employeeName) 21 this.employeeName = employeeName;22 23 public String getDepartment() 24 return department;25 26 public void setDepartment(String department) 27 this.department = department;28 29 public Address getAddress() 30 return address;31 32 public void setAddress(Address address) 33 this.address = address;34 35
01 package org.arpit.javapostsforlearning;02 public class Address 03 04 int homeNo;05 String street;06 String city;07 public Address(int homeNo, String street, String city) 08 super();09 this.homeNo = homeNo;10 this.street = street;11 this.city = city;12 13 public int getHomeNo() 14 return homeNo;15 16 public void setHomeNo(int homeNo) 17 this.homeNo = homeNo;18 19 public String getStreet() 20 return street;21 22 public void setStreet(String street) 23 this.street = street;24 25 public String getCity() 26 return city;27 28 public void setCity(String city) 29 this.city = city;30 31
01 package org.arpit.javapostsforlearning;02 import java.io.FileInputStream;03 import java.io.FileOutputStream;04 import java.io.IOException;05 import java.io.ObjectInputStream;06 import java.io.ObjectOutputStream;07 08 public class SerializeDeserializeMain 09 /**10 * @author Arpit Mandliya11 */12 public static void main(String[] args) 13 14 Employee emp = new Employee();15 emp.setEmployeeId(101);16 emp.setEmployeeName('Arpit');17 emp.setDepartment('CS');18 Address address=new Address(88,'MG road','Pune');19 emp.setAddress(address);20 //Serialize21 try22 23 FileOutputStream fileOut = new FileOutputStream('employee.ser');24 ObjectOutputStream outStream = new ObjectOutputStream(fileOut);25 outStream.writeObject(emp);26 outStream.close();27 fileOut.close();28 catch(IOException i)29 30 i.printStackTrace();31
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 5/13
Run it :When you run SerializeDeserializeMain.java.You will get following output:
We got exception what went wrong.I forgot to mention,Address class must also be serializable.So you have to make Address serializable byimplement serialzable interface.
Address.java:
Run again:When you run again SerializeDeserializeMain.java.You will get following output:
Case 2:What if you don’t have access to reference object’ssource code(e.g you don’t have access to above Address
32 33 //Deserialize34 emp = null;35 try36 37 FileInputStream fileIn =new FileInputStream('employee.ser');38 ObjectInputStream in = new ObjectInputStream(fileIn);39 emp = (Employee) in.readObject();40 in.close();41 fileIn.close();42 catch(IOException i)43 44 i.printStackTrace();45 return;46 catch(ClassNotFoundException c)47 48 System.out.println('Employee class not found');49 c.printStackTrace();50 return;51 52 System.out.println('Deserialized Employee...');53 System.out.println('Emp id: ' + emp.getEmployeeId());54 System.out.println('Name: ' + emp.getEmployeeName());55 System.out.println('Department: ' + emp.getDepartment());56 address=emp.getAddress();57 System.out.println('City :'+address.getCity());58 59
1 java.io.NotSerializableException: org.arpit.javapostsforlearning.Address2 at java.io.ObjectOutputStream.writeObject0(Unknown Source)3 at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)4 at java.io.ObjectOutputStream.writeSerialData(Unknown Source)5 at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)6 at java.io.ObjectOutputStream.writeObject0(Unknown Source)7 at java.io.ObjectOutputStream.writeObject(Unknown Source)
01 import java.io.Serializable;02 03 public class Address implements Serializable04 05 int homeNo;06 String street;07 String city;08 public Address(int homeNo, String street, String city) 09 super();10 this.homeNo = homeNo;11 this.street = street;12 this.city = city;13 14 public int getHomeNo() 15 return homeNo;16 17 public void setHomeNo(int homeNo) 18 this.homeNo = homeNo;19 20 public String getStreet() 21 return street;22 23 public void setStreet(String street) 24 this.street = street;25 26 public String getCity() 27 return city;28 29 public void setCity(String city) 30 this.city = city;31 32
1 Deserialized Employee... 2 Emp id: 101 3 Name: Arpit 4 Department: CS 5 City: Pune
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 6/13
class)If you don’t have access to address class then how will you implement serializable interface in Address class.Is there any alternative to that?yes there is,You can create another class which extends address and make it serialzable but It can fails in many cases:
What if class is declared as final
What if class have reference to other non serializable object.
So then how will you serialize Employee object? so solution is you can make it transient.If you don’t want to serialize any field then make ittransient.
So after making address transient in Employee class when you run program.You will get nullPointerException because during deserializationaddress reference will be null
Case 3:What if you still want to save state of referenceobject(e.g above address object):If you make address transient then during deserialization it will return null.But what if you still want to have same state as when you haveserialized address object.Java serialization provides a mechnism such that if you have private methods with particular signature then they willget called during serialization and deserialization so we will override writeObject and readObject method of employee class and they will becalled during serialization and deserialization of Employee object.
Employee.java:
One thing should be kept in mind that ObjectInputStream should read data in same sequence in which we have written data toObjectOutputStream. Create Address.java in org.arpit.javapostsforlearning:
Address.java:
1 transient Address address
01 package org.arpit.javapostsforlearning;02 import java.io.IOException;03 import java.io.ObjectInputStream;04 import java.io.ObjectOutputStream;05 import java.io.Serializable;06 07 public class Employee implements Serializable08 09 int employeeId;10 String employeeName;11 String department;12 transient Address address;13 14 public int getEmployeeId() 15 return employeeId;16 17 public void setEmployeeId(int employeeId) 18 this.employeeId = employeeId;19 20 public String getEmployeeName() 21 return employeeName;22 23 public void setEmployeeName(String employeeName) 24 this.employeeName = employeeName;25 26 public String getDepartment() 27 return department;28 29 public void setDepartment(String department) 30 this.department = department;31 32 public Address getAddress() 33 return address;34 35 public void setAddress(Address address) 36 this.address = address;37 38 39 private void writeObject(ObjectOutputStream os) throws IOException, ClassNotFoundException40 41 try 42 os.defaultWriteObject();43 os.writeInt(address.getHomeNo());44 os.writeObject(address.getStreet());45 os.writeObject(address.getCity());46 47 catch (Exception e)48 e.printStackTrace(); 49 50 51 private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException52 53 try 54 is.defaultReadObject();55 int homeNo=is.readInt();56 String street=(String) is.readObject();57 String city=(String) is.readObject();58 address=new Address(homeNo,street,city);59 60 catch (Exception e) e.printStackTrace(); 61 62
01 package org.arpit.javapostsforlearning;02 import java.io.Serializable;
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 7/13
Create SerializeDeserializeMain.java in org.arpit.javapostsforlearning:
SerializeDeserializeMain.java:
Run it :When you run SerializeDeserializeMain.java.You will get following output:
03 04 public class Address 05 06 int homeNo;07 String street;08 String city;09 10 public Address(int homeNo, String street, String city) 11 super();12 this.homeNo = homeNo;13 this.street = street;14 this.city = city;15 16 public int getHomeNo() 17 return homeNo;18 19 public void setHomeNo(int homeNo) 20 this.homeNo = homeNo;21 22 public String getStreet() 23 return street;24 25 public void setStreet(String street) 26 this.street = street;27 28 public String getCity() 29 return city;30 31 public void setCity(String city) 32 this.city = city;33 34
01 package org.arpit.javapostsforlearning;02 import java.io.FileInputStream;03 import java.io.FileOutputStream;04 import java.io.IOException;05 import java.io.ObjectInputStream;06 import java.io.ObjectOutputStream;07 08 public class SerializeDeserializeMain 09 /**10 * @author Arpit Mandliya11 */12 public static void main(String[] args) 13 14 Employee emp = new Employee();15 emp.setEmployeeId(101);16 emp.setEmployeeName('Arpit');17 emp.setDepartment('CS');18 Address address=new Address(88,'MG road','Pune');19 emp.setAddress(address);20 //Serialize21 try22 23 FileOutputStream fileOut = new FileOutputStream('employee.ser');24 ObjectOutputStream outStream = new ObjectOutputStream(fileOut);25 outStream.writeObject(emp);26 outStream.close();27 fileOut.close();28 catch(IOException i)29 30 i.printStackTrace();31 32 33 //Deserialize34 emp = null;35 try36 37 FileInputStream fileIn =new FileInputStream('employee.ser');38 ObjectInputStream in = new ObjectInputStream(fileIn);39 emp = (Employee) in.readObject();40 in.close();41 fileIn.close();42 catch(IOException i)43 44 i.printStackTrace();45 return;46 catch(ClassNotFoundException c)47 48 System.out.println('Employee class not found');49 c.printStackTrace();50 return;51 52 System.out.println('Deserialized Employee...');53 System.out.println('Emp id: ' + emp.getEmployeeId());54 System.out.println('Name: ' + emp.getEmployeeName());55 System.out.println('Department: ' + emp.getDepartment());56 address=emp.getAddress();57 System.out.println('City :'+address.getCity());58 59
1 Deserialized Employee...2 Emp id: 1013 Name: Arpit4 Department: CS5 City :Pune
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 8/13
so now we got same state of address object as it was before serialization.
Inheritance in Serialization:Now we will see how inheritance affects serialization.So there can be muliple cases whether super class is serializable or not.If not then howwill you handle that and how it works.Lets see by example. We will create Person.java which will be superclass of Employee:
Case 4: What if superclass is Serializable?If superclass is serialzable then all its subclasses are automatically serializable.
Case 5:What if superclass is not Serializable?If super class is not serializable then we have to handle it quite differently.
If superclass is not serializable then it must have no argument constructor.
Person.java
Create Employee.java in org.arpit.javapostsforlearning:
Employee.java:
01 package org.arpit.javapostsforlearning;02 public class Person 03 04 String name='default';05 String nationality;06 07 public Person()08 09 System.out.println('Person:Constructor');10 11 12 public Person(String name, String nationality) 13 super();14 this.name = name;15 this.nationality = nationality;16 17 18 public String getName() 19 return name;20 21 22 public void setName(String name) 23 this.name = name;24 25 26 public String getNationality() 27 return nationality;28 29 30 public void setNationality(String nationality) 31 this.nationality = nationality;32 33 34
01 package org.arpit.javapostsforlearning;02 import java.io.Serializable;03 04 public class Employee extends Person implements Serializable05 06 int employeeId;07 String department;08 09 public Employee(int employeeId,String name,String department,String nationality)10 11 super(name,nationality);12 this.employeeId=employeeId;13 this.department=department;14 System.out.println('Employee:Constructor');15 16 17 public int getEmployeeId() 18 return employeeId;19 20 public void setEmployeeId(int employeeId) 21 this.employeeId = employeeId;22
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 9/13
Create SerializeDeserializeMain.java in org.arpit.javapostsforlearning:
SerializeDeserializeMain.java:
Run it :When you run SerializeDeserializeMain.java.You will get following output:
23 24 public String getDepartment() 25 return department;26 27 public void setDepartment(String department) 28 this.department = department;29 30
01 package org.arpit.javapostsforlearning;02 import java.io.FileInputStream;03 import java.io.FileOutputStream;04 import java.io.IOException;05 import java.io.ObjectInputStream;06 import java.io.ObjectOutputStream;07 08 public class SerializeDeserializeMain 09 10 /**11 * @author Arpit Mandliya12 */13 public static void main(String[] args) 14 15 //Serialize16 Employee emp = new Employee(101,'Arpit','CS','Indian');17 System.out.println('Before serializing');18 System.out.println('Emp id: ' + emp.getEmployeeId());19 System.out.println('Name: ' + emp.getName());20 System.out.println('Department: ' + emp.getDepartment());21 System.out.println('Nationality: ' + emp.getNationality());22 System.out.println('************');23 System.out.println('Serializing');24 try25 26 FileOutputStream fileOut = new FileOutputStream('employee.ser');27 ObjectOutputStream outStream = new ObjectOutputStream(fileOut);28 outStream.writeObject(emp);29 outStream.close();30 fileOut.close();31 catch(IOException i)32 33 i.printStackTrace();34 35 36 //Deserialize37 System.out.println('************');38 System.out.println('Deserializing');39 emp = null;40 try41 42 FileInputStream fileIn =new FileInputStream('employee.ser');43 ObjectInputStream in = new ObjectInputStream(fileIn);44 emp = (Employee) in.readObject();45 in.close();46 fileIn.close();47 catch(IOException i)48 49 i.printStackTrace();50 return;51 catch(ClassNotFoundException c)52 53 System.out.println('Employee class not found');54 c.printStackTrace();55 return;56 57 System.out.println('After serializing');58 System.out.println('Emp id: ' + emp.getEmployeeId());59 System.out.println('Name: ' + emp.getName());60 System.out.println('Department: ' + emp.getDepartment());61 System.out.println('Nationality: ' + emp.getNationality());62 63
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 10/13
If superclass is not Serializable then all values of the instance variables inherited from super class will be initialized by calling constructor ofNonSerializable Super class during deserialization process. So here name is inherited from person so during deserialization,name is initializedto default.
Case 6What if superclass is Serializable but you don’t wantsubclass to be SerializableIf you don’t want subclass to serializable then you need to implement writeObject() and readObject() method and need to throwNotSerializableException from this methods.
Case 7Can you Serialize static variables?No,you can’t.As you know static variable are at class level not at object level and you serialize a object so you can’t serialize static variables.
Summary:Serialization is the translation of your Java object’s values/states to bytes to send it over network or save it.On other hand,Deserialization isconversion of byte code to corresponding java objects.
Good thing about Serialization is entire process is JVM independent, meaning an object can be serialized on one platform and deserializedon an entirely different platform.\
If you want to serialize any class then it must implement Serializable interface which is marker interface.
Marker interface in Java is interface with no field or methods or in simple word empty interface in java is called marker interface
serialVersionUID is used to ensure that same object(That was used during Serialization) is loaded during Deserialization.serialVersionUIDis used for version control of object.
When you serialize any object and if it contain any other object reference then Java serialization serialize that object’s entire object graph.
If you don’t want to serialize any field,then make it trasient.
If superclass is Serializable then its subclasses are automatically Serializable.
If superclass is not Serializable then all values of the instance variables inherited from super class will be initialized by calling constructor ofNonSerializable Super class during deserialization process.
If you don’t want subclass to serializable then you need to implement writeObject() and readObject() method and need to throwNotSerializableException from this methods.
You can’t serialize static variables.
Reference: Serialization in java from our JCG partner Arpit Mandliya at the Java frameworks and design patterns for beginnersblog.
Tagged with: SERIALIZATION
Do you want to know how to develop your skillset to become a JavaRockstar?
Subscribe to our newsletter to start Rocking right now!To get you started we give you our best selling eBooks for FREE!1. JPA Mini Book2. JVM Troubleshooting Guide3. JUnit Tutorial for Unit Testing4. Java Annotations Tutorial5. Java Interview Questions6. Spring Interview Questions7. Android UI Design
and many more ....
Email address:
Your email address
Sign up
13 COMMENTS
October 25th, 2013 at 2:03 pmAbhijeet Rai
Excellent post Arpit !! Its too verbose, infact. Can you please elaborate the case 3 . When exactly the writeObject/readObject methodsare called. Because, we are assuming that we have only one transient variable Address. What if we have some other transient variablesay Supervisor(int empId,String name). ? How will we serialize the Employee object. Thanks in advance.
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 11/13
Reply
November 23rd, 2013 at 8:27 pmRajesh halder
thank you it was very helpful . after long time i became able to understand about the serialization and all of that .thanks onesmore
Reply
October 27th, 2014 at 6:11 pmNitin Ramachandra
Kudos man..!!!! You’ve done a great job by specifying certain things in a very precise manner. A one stop for all the necessaryinformation on Serialization in Java.. !!!
Reply
October 29th, 2014 at 10:58 amJAVA
Awesome post !!!!!
Reply
December 17th, 2014 at 2:22 pmChetan Patel
very nicely explained, bravo.
Reply
January 4th, 2015 at 2:16 pmDilip Singh Virk
Hi ,You explained very well about serialization process in java it just cleared my douts.
Thanks
Reply
February 20th, 2015 at 9:53 amAkash
Hi Arpit,
Indeed excellent post, However I’m still not able to find the answer which an interviewer asked to me(related to Java Serialization) andI couldn’t convince him. I’ve searched many blogs but not getting any leads.
Interviewer asked me “Let say you have a Employee class having 3 properties(name, company, salary) where salary is transientvariable. When you serialize Employee object, salary will not be stored into serialized file which can be evident by inspecting the file(although the content of the file will be binary but you can see see the property name inside it and surely you wont see salary there).Now when you deserialized this Employee object back and try to get the value of name,company and salary you will get the null valuefor salary as expected. My question is how you can even refer the attribute(e.g. salary) which was not there in serialized file, As therewas no existence of salary field in returned employee object during deserialization process so dont you think you should getNullPointerException rather than Null ? ”
Though I was pretty confident that transient field never get stored and trying to get the value of this value in deserialized object willreturn null but I was not able to logically answer why it shouldn’t give NullPointerException rather null value.
I am sure I’m missing something but not able to get where I’m wrong :(
Reply
June 18th, 2015 at 8:32 pmVenkitesh
What I understood is that you are serializing the whole object with a condition that the transient variable’s value should not betaken with. That’s the reason why we get a null. But I dont know why you thought that there should be a null pointer exceptionthat should come up. Because while deserialization, you get the employee object which is not null. So a dot operator on a nonnull object will not definitely give you a null pointer exception.Eg: u deserialize the object to get Employee emp which is not null.emp.get salary or emp.get name or whatever will not give u a null pointer as per my understanding.
Reply
March 9th, 2015 at 5:58 pmkdj
every example of serializing is to a file…. i want to serialize my object (implements serializable) to a String. (please dont ask why oroffer alternatives) I dont have access to the tostring code for the object.
Can anyone post example code for this??
Reply
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 12/13
May 1st, 2015 at 6:05 amraji
I believe the Case 1 is NOT true. The Address should implement serializable in order to serialize the Employee obj. I tested it in mymachine.
Reply
June 18th, 2015 at 8:24 pmVenkitesh
Right.. The example already says that. But either the description of case 1 or example has gone wrong.
Reply
June 18th, 2015 at 8:22 pmVenkitesh
Yes… The above said comment is true. Both the case 1 description and the example contradicts to each other.Could you make it clear and correct?Anyway a wonderful effort.
Reply
July 16th, 2015 at 4:13 pmpramod vidyagar
HI,That’s a very nice document on Serialization.Please add writeReplace and readResolve methods also to make it complete.
Reply
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
Name *
Email *
Website
6 × = fifty four
Notify me of followup comments via email. You can also subscribe without commenting.
Sign me up for the newsletter!
Post Comment
KNOWLEDGE BASE
Academy
Examples
Library
Resources
Tutorials
Whitepapers
PARTNERS
Mkyong
HALL OF FAME
“Android Full Application Tutorial” series
11 Online Learning websites that youshould check out
Advantages and Disadvantages of CloudComputing – Cloud computing pros andcons
Android Google Maps Tutorial
Android JSON Parsing with Gson Tutorial
Android Location Based ServicesApplication – GPS location
Android Quick Preferences Tutorial
ABOUT JAVA CODE GEEKS
JCGs (Java Code Geeks) is an independent online community focused on creating theultimate Java to Java developers resource center; targeted at the technical architect,technical team lead (senior developer), project manager and junior developers alike.JCGs serve the Java, SOA, Agile and Telecom communities with daily news written bydomain experts, articles, tutorials, reviews, announcements, code snippets and opensource projects.
DISCLAIMER
All trademarks and registered trademarks appearing on Examples Java Code Geeks arethe property of their respective owners. Java is a trademark or registered trademark ofOracle Corporation in the United States and other countries. Examples Java Code Geeksis not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
29/8/2015 Serialization in java
http://www.javacodegeeks.com/2013/03/serializationinjava.html 13/13
THE CODE GEEKS NETWORK
.NET Code Geeks
Java Code Geeks
Web Code Geeks
Difference between Comparator andComparable in Java
GWT 2 Spring 3 JPA 2 Hibernate 3.5Tutorial
Java Best Practices – Vector vs ArrayListvs HashSet
Java Code Geeks and all content copyright © 20102015, Exelixis Media P.C. | Terms of Use | Privacy Policy | Contact
top related