Women in Technology

Hear us Roar



Article:
  Seven Low-Cost Ways to Improve Legacy Code
Subject:   using final with Serializable classes
Date:   2004-04-29 00:58:42
From:   andyp
I have a question about declaring member variables to be final in immutable classes. I agree with you that it is a trick to help the compiler enforce your own logic about the class. However when it comes to serializing these classes there is a problem. If I implement the serialization method readObject(ObjectOutputStream os), then all of the member variables that I will be setting in this method must be non-final. Do you have any suggestinos about how to have final methods in this case (without using readResolve)?
Full Threads Oldest First

Showing messages 1 through 3 of 3.

  • using final with Serializable classes
    2004-04-29 16:20:44  Robert Simmons, Jr. | O'Reilly Author [View]

    Hmm, interesting question. However, since String uses final for its data member, I immagine the problem is solvable. I will have to play around with it.

    I will get back to you. =)
    • using final with Serializable classes
      2005-04-13 14:34:32  Mith [View]

      Try this on for size:

      import java.io.Serializable;

      /**
      * "Typesafe enum" for the different document indicators associated with
      * a transaction.
      *


      * Copyright 2003 Southwest Airlines
      */

      public final class DocumentIndicator implements Serializable, Comparable
      {
      private static int nextOrdinal = 0;

      private int ordinal;
      private transient String name;

      public static final DocumentIndicator SALES =
      new DocumentIndicator("NA");
      public static final DocumentIndicator SALES_EXCHANGES =
      new DocumentIndicator("NE");
      public static final DocumentIndicator POSITIVE_ADJUSTMENTS =
      new DocumentIndicator("NY");
      public static final DocumentIndicator TICKET_BY_MAIL_SALES =
      new DocumentIndicator("TA");
      public static final DocumentIndicator REFUNDS =
      new DocumentIndicator("NR");
      public static final DocumentIndicator NEGATIVE_ADJUSTMENTS =
      new DocumentIndicator("NX");
      public static final DocumentIndicator TICKET_BY_MAIL_REFUNDS =
      new DocumentIndicator("TR");
      public static final DocumentIndicator OLD_REFUNDS =
      new DocumentIndicator("RF");

      private static final DocumentIndicator[] PRIVATE_VALUES =
      new DocumentIndicator[]
      {
      SALES, SALES_EXCHANGES, POSITIVE_ADJUSTMENTS, TICKET_BY_MAIL_SALES,
      REFUNDS, NEGATIVE_ADJUSTMENTS, TICKET_BY_MAIL_REFUNDS, OLD_REFUNDS
      };
      /**
      * All instances are controlled by the class.
      */

      private DocumentIndicator(String name)
      {
      this.name = name;
      this.ordinal = nextOrdinal++;
      }
      public int compareTo(Object o)
      {
      DocumentIndicator other = (DocumentIndicator) o;

      return name.compareTo(other.name);
      }
      /**
      * Returns the DocumentIndicator associated with the given string.
      *
      * @param indicator the String to try to match against the document
      * indicators
      * @return the DocumentIndicator associated with the given String
      */

      public static DocumentIndicator getIndicatorFrom(String indicator)
      {
      if ("NA".equals(indicator))
      {
      return SALES;
      }
      else if ("NE".equals(indicator))
      {
      return SALES_EXCHANGES;
      }
      else if ("NY".equals(indicator))
      {
      return POSITIVE_ADJUSTMENTS;
      }
      else if ("TA".equals(indicator))
      {
      return TICKET_BY_MAIL_SALES;
      }
      else if ("NR".equals(indicator))
      {
      return REFUNDS;
      }
      else if ("NX".equals(indicator))
      {
      return NEGATIVE_ADJUSTMENTS;
      }
      else if ("TR".equals(indicator))
      {
      return TICKET_BY_MAIL_REFUNDS;
      }
      else if ("RF".equals(indicator))
      {
      return OLD_REFUNDS;
      }
      else
      {
      return null;
      }
      }
      private Object readResolve() throws java.io.ObjectStreamException
      {
      return PRIVATE_VALUES[ordinal];
      }
      /**
      * Returns a String that represents the value of this object.
      *
      * @return a string representation of the receiver
      */

      public String toString()
      {
      return name;
      }
      }

    • using final with Serializable classes
      2004-04-29 16:22:38  Robert Simmons, Jr. | O'Reilly Author [View]

      Incidentally, for those that dont know, I am Robert Simmons and Kraythe. Kraythe is my internet nickname.