2

I am trying to declare a message protocol that I want to use for exchanging data over UDP. So I would like to declare a bunch of string constants in another file so that I can reference them in my main activity when I am building a packet. I know this isn't right because I am getting errors, but this is what I would like to do...

public final class PACKET
{

    // Packet Protocol Version
    public final void REV ()
    {
        final String R_1_00                         = "1.00";
        final String R_1_01                         = "1.01";
    }

    public final void CMD ()
    {
        final String STAT                           = "STAT";
        final String STOP                           = "STOP";
        final String WAIT                           = "WAIT";

    }
}

That way when I am creating a packet in my main activity I can use PACKET.CMD.WAIT, or something like that. Is there a proper way to do this? Thanks

Willis
  • 5,308
  • 3
  • 32
  • 61
  • 1
    This is one way to do it. Make your variables `public` so that you can access then directly like `PACKET.R_1_00`. Also you can try enums. http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – Aditya Peshave Nov 07 '14 at 18:28
  • 1
    http://stackoverflow.com/questions/66066/what-is-the-best-way-to-implement-constants-in-java - Has answers you are looking for. – BatScream Nov 07 '14 at 18:31
  • Is there any reason not to use the android strings resources file for this? Or is it just a preference? – Spaceman Spiff Nov 07 '14 at 18:31
  • I think as per the answer in the above question and the codes I have seen so far, public classes are most famous with final strings. – Aditya Peshave Nov 07 '14 at 18:32
  • Normally resources files contain variables that can be altered/injected by the user manually to change the application code flow. so constants are not stored in resource files. – BatScream Nov 07 '14 at 18:34

2 Answers2

1

You could do this:

public final class PACKET
{
    // Packet Protocol Version
    public static class REV {
        public final String R_1_00 = "1.00";
        public final String R_1_01 = "1.01";
    }

    public static class CMD {
        public final String STAT = "STAT";
        public final String STOP = "STOP";
        public final String WAIT = "WAIT";
    }
}
Rob Meeuwisse
  • 2,847
  • 1
  • 17
  • 21
  • This is what I was originally trying to do. I see I had forgotten to make the internal classes static. Thanks – Willis Nov 07 '14 at 19:08
0

Personally I would use an Enum for this.

public final class Packet {

    // Packet Protocol Version
    public static enum REV {
        R_1_00("1.00"),
        R_1_01("1.01");

        private final String name;

        private REV(String name){
            this.name = name;
        }

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

    public static enum CMD {
        STAT, STOP, WAIT;
    }
}
user2336315
  • 15,697
  • 10
  • 46
  • 64