0

I am trying to send a object of the following class using java sockets:

public class CommunicationObj implements Serializable{
    private String ID;
    public AuthenticationParams s = new AuthenticationParams();
    public CommunicationObj(String s){
        ID = s;
    }
    public String getID(){
        return ID;
    }
}

But sending an object of the following class raises exception (unable to send the object), but the following code works

public class CommunicationObj implements Serializable{
    private String ID;

    public CommunicationObj(String s){
        ID = s;
    }
    public String getID(){
        return ID;
    }
}

Why AuthenticationParams object is creating such a problem here? Any help will be appreciated. Note: All the classes and packages used are identical to both server and client.

3
  • 3
    Is AuthenticationParams also Serializable? Commented Dec 23, 2013 at 13:27
  • In the future use the code button - "{}" - to format your code rather than <code> tags. Commented Dec 23, 2013 at 13:30
  • VincentRamdhanie, that solves the problem :) Commented Dec 23, 2013 at 13:38

2 Answers 2

1

If any part of AuthenticationParams or AuthenticationParams itself is not marked as serializable, your serialization will fail.

In fact, every part of every part of your class must be serializable, or fields that for some reason cannot be serialized should be given the transient modifier, which indicates that that object should not be included in the serialization process.

Sign up to request clarification or add additional context in comments.

Comments

0

AuthenticationParams class might not be serializable. You can add transient modifier to discard it from serialization like:

public transient AuthenticationParams s = new AuthenticationParams();

But if you want to include this object in the serialized form then you have no way but make AuthenticationParams class serializable.

Rule of Serialization: All non-transient objects referenced from the instance (the object instance which you want to serialize) must also be serializable.

Not: You can make use of java.io.Externalizable interface to develop your custom serialization mechanism.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.