Reading and Writing Your Own Objects

You can also customize your objects to have the capability to be serialized just as you did with the Date class.

Listing 22.4 shows the source code for serializing an example class called Employee. Because all the data within the class can be serialized, all you had to do in this case was to implement the java.io.Serializable interface.

Code Listing 22.4. Source Code for Employee.java
 public class Employee implements java.io.Serializable { // Private instance variables for an employee private String firstName = null; private String lastName = null; private float payRate; private int hoursWorked; // Constructor public Employee( String first, String last, float rate, int hours ) { super(); firstName = first; ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.