A definition of Key suitable for object serialization.

JAVA PRIMER
Using Java Object Serialization: A Few Pitfalls
Huw Evans
Listing 4. A definition of Key suitable for object serialization.


public class ObjId
    {
      private static int ctr = 0;

      public static int allocate()
      {
        return ++ctr;
      }
    }

    public class Key implements java.io.Serializable
    {
      int obj_id;

      public Key()
      {
        obj_id = ObjId.allocate();
      }

      public String toString()
      {
        return "I am a Key";
      }

      public int hashCode()
      {
        return obj_id;
      }

      public boolean equals(Object o)
      {
        return obj_id == ((Key) o).obj_id;
      }
    }

Featured

Upcoming Events

AppTrends

Sign up for our newsletter.

I agree to this site's Privacy Policy.