Having a hard time understanding enum "instantiation"?

60 views Asked by At

So I have these classes that I can't change that I need to use in order to send serialized objects across to a server. But I don't have much experience with enums and am having a hard time understanding how to do this?

import java.io.Serializable;

public abstract class Message implements Serializable {

    private static final long serialVersionUID = 0L;

    private final MessageType type;

    public Message(MessageType type) {
        this.type = type;
    }

    public MessageType getType() {
        return type;
    }

@Override
    public String toString() {
        return type.toString();
    }
}



public final class CommandMessage extends Message {

    private static final long serialVersionUID = 0L;

    private final Command cmd;

    public CommandMessage(Command cmd) {
        super(MessageType.COMMAND);
        this.cmd = cmd;
    }

    public Command getCommand() {
        return cmd;
    }

    public static enum Command {

        LIST_PLAYERS, EXIT, SURRENDER;

        private static final long serialVersionUID = 0L;
    }
}

I mostly understand the serialization, this is for a simple tictactoe game, and i have a background thread running to take in objects. But how can I make a command to send to the server? Let's say i want to see a list of players, how do I MAKE the commandMessage object so i can send it? I think I'm missing something really simple >_<

public static void main(String[]args) throws IOException{

    //make tictactoe client
    TicTacToeClient newClient = new TicTacToeClient();

    //start run
    newClient.run();    

    //start a connection, send username
    ConnectMessage connect = new ConnectMessage("User17");  
    newClient.out.writeObject(connect);

    CommandMessage newComm = new CommandMessage(); //what!? HOW?
    //s.BOARD = PLAYERLIST;???

    //NOPE.
    //PlayerListMessage playerList = new PlayerListMessage();               
    System.out.println();       
}
1

There are 1 answers

0
rickyalbert On BEST ANSWER

You must use the constructor

CommandMessage newComm = new CommandMessage(CommandMessage.Command.LIST_PLAYERS);

or use one of the other enums provided