Results 1 to 9 of 9
  1. #1
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default Serialization query

    Hi,
    I have a class A that is serializable. This particular class contains three Strings and an instance of class B as its member variables (class B is not serializable). When i will try to serialize class A, it will throw an java.io.NotSerializableException because class B is not serializable. I can't even place the modifier 'transient' before the instance of the class B. Any ideas, how to go with this situation ?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Look at the Serializable interface API. It contains methods you can override. In those methods, you would handle serializing each field that can be serialized. I *think* this is just a matter of writing each of the fields to the object output stream or reading them back in from it.

  3. #3
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Had it been allowed to place the 'transient' identifier before the class B's instance, it would have been great. Why is it not allowed ? If you see you can place the 'transient' identifier before the instances of String class and make it escape the process of serialization. Then why not before our own classes :(

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You're right, the transient keyword should fix the situation. You shamed me into opening Eclipse and creating a test.

    Java Code:
    package test;
    
    import java.io.ByteArrayOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    public class SerialTest {
    	public static void main(String[] args) {
    		SerialTest instance = new SerialTest();
    		instance.run();
    	}
    
    	public void run() {
    		ClassA classA = null;
    		final ObjectOutputStream oos;
    		classA = new ClassA();
    		try {
    			oos = new ObjectOutputStream(new ByteArrayOutputStream());
    			oos.writeObject(classA);
    		}
    		catch (Throwable t) {
    			t.printStackTrace();
    		}
    	}
    }
    
    class ClassA
        implements
        Serializable {
    	/** For serialization, stops compiler warning */
    	private static final long serialVersionUID = 1L;
    	private String _String = "Initial value";
    	private [B][COLOR="Red"]/*transient*/[/COLOR][/B] ClassB _ClassB = new ClassB("First value");
    
    	public void setString(String string) {
    		_String = string;
    	}
    
    	public String getString() {
    		return _String;
    	}
    
    	public void setClassB(ClassB classB) {
    		_ClassB = classB;
    	}
    
    	public ClassB getClassB() {
    		return _ClassB;
    	}
    }
    
    class ClassB {
    	private final String _FinalString;
    
    	public ClassB(final String pString) {
    		_FinalString = pString;
    	}
    
    	public String getFinalString() {
    		return _FinalString;
    	}
    }
    As is, the code will run but cause a not serializable exception, because I commented out the transient keyword. Remove the /*, */ and the exception goes away.

    Copy and paste the above and make sure it runs for you...

  5. #5
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Your code definitley worked and the idea of placing the 'transient' identifier was successfull. But somehow, placing the 'transient' identifier in my code gives compilation error. I am explaining my code:

    One serializable class:

    package com.test;

    import java.io.Serializable;

    public class SerializableClass implements Serializable {

    static final long serialVersionUID = 1L;
    String str1 = "Sun";
    String str2 = "MicroSystems";
    }

    One Non-serializable class:

    package com.test;

    public class NonSerializableClass {

    String one = "one";
    String two = "two";
    }

    The main class:

    package com.test;

    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;


    public class Serl {

    public static void main(String[] args) {
    SerializableClass serializableClass = new SerializableClass();
    /*transient*/ NonSerializableClass nonSerializableClass = new NonSerializableClass();
    FileOutputStream fos = null;
    ObjectOutputStream out = null;

    try {
    fos = new FileOutputStream("D:\\Test\\MyFile");
    out = new ObjectOutputStream(fos);
    out.writeObject(serializableClass);
    out.writeObject(nonSerializableClass);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }


    Placing the 'transient' identifier before the NonSerializableClass instance gives a compilation error. Any ideas Sir ?

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    OK, I saw some things.

    If you look at my example, ClassA contains an instance of ClassB. The main class, SerialTest, serializes ClassA.

    *Perhaps* a class can serialize itself, and I can see situations where that would make sense. But a more typical approach would be to have a main logic class that serializes other classes and does something with the stream.

    Your example serializes both ClassA and ClassB, and ClassA does not contain a reference to ClassB. Therefore, the first writeObject() should work, where the second one can't, because ClassB is not serializable.

    Look again at my example, at the structure of the three classes. I based it on your original description.

    --

    In your example, you defined your two fields as *local* variables in the main() method. That is why you are getting the error. Only instance variables can be marked transient.

    --

    I've gone through this frustration before, where my lack of understanding of the technology leads me to make basic mistakes in the structure of my classes.

    Go back and think through what you want to accomplish, and how the class design gets the job done. Then worry about the transient keyword.

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Why are you trying to serialize a nonserializable object? This isn't going to work:

    Java Code:
    out.writeObject(nonSerializableClass);

  8. #8
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Steve - I got the point. I pasted the code which is somewhat (rather quite) different from the problem I mentioned. None the less, I got the point that in both the cases I was trying to place the "transient" keyword before the *local* variable which is not allowed. In both the cases placing the "transient" keyword before the instance variable works fine. Really had a nice time on this thread.

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Good! It's funny trying to answer questions, because we have to guess exactly what the requester is trying to do. But, the best thing is when we convey some concepts, and the requester understands and then applies them...

Similar Threads

  1. Java serialization
    By paul in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 08:58 PM
  2. about serialization
    By bishnu in forum New To Java
    Replies: 0
    Last Post: 12-19-2008, 09:13 AM
  3. Serialization/Deserialization Error
    By andrepezzo in forum Networking
    Replies: 0
    Last Post: 12-16-2008, 04:21 PM
  4. Need help using serialization
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 08:23 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •