7 Replies Latest reply: Apr 3, 2009 4:06 AM by r035198x RSS

    Serialization & transient attributes

    843789
      Hello,
      I'm very new to Java programming and this site, so I apologize if this question has already been asked numerous times. I have to do a project for my intro to java class that is suppose to have binary file I/O in all of our classes we have to do. All of the classes are serializable, however, one of the classes extends another class which is not serializable. I thought that if I put the transient keyword in front of all the calls to the super class that that would allow for proper serialization but when I try to compile my code it tells me "illegal start of expression" if i do something like say :

      public Example(){
      transient super();
      this.attribute1=" ";
      this.attribute2=" ";}

      So does this mean that I can call on the super class constructor when it's not serializable in its subclass that is serializable and there won't be a problem?

      another question that is an extension of the first:

      This super class has two properties for example String title and int numTitles so would you ever need to have attributes in the subclass that account for these properties? If so is the representation below appropriate? When I compiled it I didn't receive any errors.

      transient MainExample title;
      transient MainExample numTitles;

      I understand the concept of serialization but I still don't seem to get how to work with it completely, especially with dealing with nonserializable properties as well. So if anyone can help me get this concept and how to take it and translate it into proper coding/tell me why what I have here is wrong, I would GREATLY appreciate it!
        • 1. Re: Serialization & transient attributes
          843789
          1. Hello and welcome to the Sun Java forums!

          2. transient super(); ? The transient keyword does not go in front of method calls; in fact, it must not be in an expression at all. The concept of "transient method calls" should not exist in your memory anymore from this point onwards.

          3. The transient keyword goes in your instance field declarations to avoid them being serialised.

          4. To serialise the fields of superclasses, subclasses should implement a writeObject method according to the Serializable interface's documentation, containing something like:
            out.defaultWriteObject();
            out.writeInt(super.field1);
            out.writeObject(super.stringfield);
            // etc.
          s
          • 2. Re: Serialization & transient attributes
            r035198x
            Please read this serialization article.
            • 3. Re: Serialization & transient attributes
              843789
              Thanks for clearing up what exactly transient should be in front of, which makes sense because we also have to override the writeObject and readObject method calls and make appropriate changes to these now that the class extends a nonserializable class.

              In your #4 you said to serialize properties of a super class follow the example code, so does that mean even if the super class isn't serializable some of its properties can still be?

              We originally used text file I/O in the project before this and now we've been asked to change these reads and writes with the binary file I/O, like I stated before, so I've already done that with the method readObject() & writeObject(Object obj) already defined in for ObjectInputStream and ObjectOutputStream. So to override these calls I must pass in the properties defined in the current class and just omit the properties in the super class? or do i just place them in here and put them in as transient? or still define them as I have placed here?

              private void writeObject(java.io.ObjectOutputStream oos){
              oos.writeBoolean(this.someproperty);
              oos.writeDouble(this.someproperty);
              oos.wrtieInt(this.someproperty);
              oos.writeObject(this.someproperty) ; *this would be in place for the attribute of an array of objects. I'm not sure that this is allowed seeing as how it's inside of a writeObject

              oos.writeInt(super.getSomeProperty());
              oos.writeUTF(super.getSomeProperty());
              }
              • 4. Re: Serialization & transient attributes
                843789
                Thanks! This link helped.
                • 5. Re: Serialization & transient attributes
                  843789
                  nkd07 wrote:
                  Thanks for clearing up what exactly transient should be in front of, which makes sense because we also have to override the writeObject and readObject method calls and make appropriate changes to these now that the class extends a nonserializable class.
                  You're welcome.
                  In your #4 you said to serialize properties of a super class follow the example code, so does that mean even if the super class isn't serializable some of its properties can still be?
                  Yes. If the type of the field (or property) implements Serializable or extends something that implements Serializable (e.g. String is Serializable, so is Integer because it extends Number, but OutputStream is not Serializable).
                  [...] So to override these calls I must pass in the properties defined in the current class and just omit the properties in the super class? or do i just place them in here and put them in as transient? or still define them as I have placed here?
                  No, defaultWriteObject() takes care of the properties for the class it's defined in and any subclasses you make in the future. You just need to write super's properties. (Same for readObject and defaultReadObject).

                  I also suggest reading that link (Reply #2) and experimenting with the methods.

                  s
                  • 6. Re: Serialization & transient attributes
                    r035198x
                    Don't forget to call the defaultRead/Write methods if you want the serialization mechanism to do part of the work for you.
                    • 7. Re: Serialization & transient attributes
                      843789
                      Thank you so much for the help, I get it a lot more.