0

I have two enums (Enum1 and Enum2) that both implement an interface (Face).

Both enums also have fields (Image img).

The way the interface is implemented is different on each enum.

Then I have a serializable class (Message) that has a field called payload that is a Face.

When i send a Message through a socket which the value of its payload is of Enum1 everything works OK. But when i do the same thing with Enum2 I get a not serializable exception for the image field on that Enum.

There is a bunch of code related to all of this and i know that the mistake could be hidden in it but its a clear Exception so my question is: How does java handle sending interfaced enums through the sockets? What will actually be sent, what will be received, and what can i cast the received payload to?

interface Face {
     void action();
}



enum Enum1 implements Face {
    AA("aa"), BB("bb"), CC("cc");

    Image img;
    Enum1(String _path){
         // init image
    }

    void action(){
        // do stuff
    }
}



enum Enum2 implements Face {
    XX("xx"), YY("yy"), ZZ("zz");

    Image img;
    Enum2(String _path){
         // init image
    }

    void action(){
        // do other different stuff
    }
}



class Message implements Serializable {
    Face face;
    Message(Face _face){
        this.face = _face;
    }
}



socket.send(new Message(Enum1.AA)); 
// Everything ok
socket.send(new Message(Enum2.XX)); 
// Not serializable exception (Image)
user207421
  • 305,947
  • 44
  • 307
  • 483
Wolforce
  • 79
  • 4
  • 1
    Can you post the stacktrace you're getting? – akortex Jul 09 '18 at 16:15
  • IMHO, you've over complicated your approach to the problem. – zlakad Jul 09 '18 at 16:20
  • 1
    You'll need to provide a [mcve]. It's unclear what your socket is, what your actual image initialization entails etc., so code that recreates the problem is essential. Java does not serialize the fields in the enum. So the source of the problem lies elsewhere. – RealSkeptic Jul 09 '18 at 16:29
  • More info needed as other comments have already indicated. Curious though - is `Image` an interface? If yes, does it extend Serializable, or do all of its concrete classes implement Serializable? – Andrew S Jul 09 '18 at 17:02
  • 1
    "But when i do the same thing with Enum2 I get a not serializable exception for the image field on that Enum." I guess your problem is that the image object in Enum2 is not serializable. The fact that its in an enum is irrelevant. – NickL Jul 09 '18 at 17:08
  • 1
    @NickL Actually, it is very relevant, because enum fields are never serialized so the non-serializable field should not have made any difference. Java serializes only the name of an enum constant, relying on the fact that when the receiving party loads the class, the constants will be initialized already. – RealSkeptic Jul 10 '18 at 08:14
  • @RealSkeptic you're right, in that case I'm curious to know why OP got this exception.. My guess is that `Enum2` is actually not an enum. – NickL Jul 10 '18 at 18:03

0 Answers0