9 Replies Latest reply: May 24, 2007 11:27 AM by 807606 RSS

    implements Serializable

    scottjhn
      I saw many classes "implements Serializable". What type of class (for what usage), must "implements Serializable"?



      Thanks


      Scott
        • 1. Re: implements Serializable
          807606
          Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

          http://java.sun.com/docs/books/tutorial/essential/io/objectstreams.html
          • 2. Re: implements Serializable
            scottjhn
            Thanks for the help.

            Actually I need some explanation which is more entry-level based, and better yet in layman's wording, or some examples and usages.



            Thanks.
            • 3. Re: implements Serializable
              807606
              Quick demo of "implements Serializable" versus not:
              import java.io.*;
              
              class S implements Serializable {}
              
              class NotS {}
              
              public class SerializableTest {
                  public static void main(String[] args) {
                      test(new S());
                      test(new NotS());
                  }
              
                  static void test(Object o) {
                      try {
                          ByteArrayOutputStream baos = new ByteArrayOutputStream(); //just for a sink
                          ObjectOutputStream out = new ObjectOutputStream(baos);
                          out.writeObject(o);
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
              • 4. Re: implements Serializable
                807606
                I'll giveyou a quick break down, since I just got done wrangling it myself.

                Serializable basically means that that class can be written to a file. If you don;t include it, it can't be translated and encrypted. It's used with pretty much all I/O (Input/Output) applications.
                • 5. Re: implements Serializable
                  807606
                  Serializable basically means that that class can be
                  written to a file.
                  to be a bit more clear, it actually turns the object into a byte stream.
                  that in turn can be written to a file or transmitted over a network, etc.
                  • 6. Re: implements Serializable
                    scottjhn
                    Thank you all for the help.

                    But one thing. I saw in some application, it has something like this:

                    public class TestForm implements Serializable {

                    List testList;
                    String test;
                    ...

                    }


                    But I did not find corresponding OutputStream and InputStream (to serialize and deserialize the object). Any idea why the TestForm (a form bean class) need to implements the Serializable? What if it does not implements the Serializable?


                    Thanks

                    Scott
                    • 7. Re: implements Serializable
                      807606
                      Perhaps the serialization was being done behind the scenes by an application framework.
                      • 8. Re: implements Serializable
                        scottjhn
                        This application will interface with a database. So all the persistent changes will be saved to a database. It does not need to use the Serialized form bean to store the data.

                        So actually I am thinking this form bean unnecessarily implmented the Serializable.


                        Scott
                        • 9. Re: implements Serializable
                          807606
                          My crystal ball is a little foggy, but you could be right.