1

I'm trying to serialize an object in its constructor that has an interface, I'll explain:

This is Object class:

public class MyObj implements Serializable{
    private static final long serialVersionUID = -29238982928391L;
    private MyCallback onMyCallback;
    private String shortcutsText;

    public MyObj(String shortcutsText, MyCallback onMyCallback){
        this.shortcutsText = shortcutsText;
        this.onMyCallback = onMyCallback;
    }

    public MyCallback getOnMyCallback() {
        return onMyCallback;
    }
}

This is MyCallback interface:

public interface MyCallback extends Serializable {
    long serialVersionUID = -1306760703066967345L;
    void onMyCallbackClickListener();
}

But when I try to write it with (out is ObjectOutputStream):

MyCallback myCallback = new MyCallback() {
            @Override
            public void onMyCallbackClickListener() {
                Toast.makeText(getApplicationContext(), "TEST!", Toast.LENGTH_LONG).show();
            }
        };
out.writeObject(myCallback);

Or with:

out.writeObject(new MyObj("Test", myCallback));

It throws:

java.io.NotSerializableException

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54

4 Answers4

1

Anonymous inner classes are not serializable. See good explanation here: NotSerializableException on anonymous class

Community
  • 1
  • 1
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • ..so how do I pass an interface between two processes asynchronously?..because AIDL not help me – Michele Lacorte Jan 11 '17 at 11:56
  • You can promote it to be real class. Then it will be serialisable. But serialising java objects and passing them around is not realy good idea anyway. Grab yourself some data binding tool, and send (for example) JSON around. – Konstantin Pribluda Jan 11 '17 at 12:19
1

interface or abstract class cannot be serialized, only concrete class that implements java.io.Serializable and primitive types can. Serialization is for transferring information/data. Even though you can write something like the following, it does not mean you can serialize an interface.

public static class MyObj implements Serializable{
    private static final long serialVersionUID = -29238982928391L;

    private String text;
    private MyCallback callback;

    MyObj(String text, MyCallback callback){
        this.text = text;
        this.callback = callback;
    }

    MyCallback getCallback(){
        return callback;
    }

}

interface MyCallback extends Serializable{
}

static class MyCallbackImpl implements MyCallback {
    private static final long serialVersionUID = -19238982928391L;
}

public static void main(String args){
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream);

    MyCallback tout = new MyCallbackImpl();
    System.out.println(tout.toString());
    out.writeObject(tout);

    MyObj obj = new MyObj("test", tout);
    out.writeObject(obj);
    System.out.println(obj.getCallback());
    out.close();
}

It's the concrete class MyCallbackImpl that was serialized actually. We can tell from the output:

Test$MyCallbackImpl@17d10166
Test$MyCallbackImpl@17d10166

As other answers have mentioned, anonymous classes, local classes and non-static inner classes cannot get serialized, according to detailed explanation here

aietcn
  • 346
  • 3
  • 9
0

"Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged". So,you can't write like this:

MyCallback myCallback = new MyCallback() { @Override public void onMyCallbackClickListener() { Toast.makeText(getApplicationContext(), "TEST!", Toast.LENGTH_LONG).show(); } };

0

You got this Exception because you can not Serialize an anonymous inner class.

Rouliboy
  • 1,377
  • 1
  • 8
  • 21