-7

I am porting a lot of code from PHP to Java (an Android application). Everything is done but I am facing a problem here,

I got this PHP code:

class SenderKeyGroupData{
  const MESSAGE = 1;
  const SENDER_KEY = 2;
  /* @var array Field descriptors */
  protected static $fields = array(
      self::MESSAGE => array(
        'name' => 'message',
        'required' => false,
        'type' => 7,
      ),
      self::SENDER_KEY => array(
          'name' => 'sender_key',
          'required' => false,
          'type' => "SenderKeyGroupMessage",
      ),
  );

However I want this code in Java, for example I created this Java code:

class SenderKeyGroupData {
    int MESSAGE = 1; // Must be const
    int SENDER_KEY = 2; // Must be const

    // These two Maps must be in the $fields array?
    Map MESSAGE = new HashMap(); //Duplicate conflict here
    MESSAGE.put("name", "message");
    MESSAGE.put("required", false); // Map can't contain booleans?
    MESSAGE.put("type", "7");

    Map SENDER_KEY = new HashMap(); //Duplicate conflict here
    SENDER_KEY.put("name", "sender_key");
    SENDER_KEY.put("required", false); // Map can't contain booleans?
    SENDER_KEY.put("type", "SenderKeyGroupMessage");
}

I described the problems as comments. All ideas are welcome.

Note that the const contain 1 and 2 as value in the constructor, but also get an array assigned. So please give an example instead of pointing it as duplicate.

gi097
  • 7,313
  • 3
  • 27
  • 49
  • If what you describe in the comments are the problems, then there is not one, but more. If it's the const question, then swidmann's comment above answers it. – Whirl Mind Nov 06 '15 at 16:28
  • 1
    This is pretty straight forward stuff, what is unclear about the error message? You can't have two variables with the same name. Once I fix that issue, your code compiles. Not sure what you're saying about "Map can't contain booleans". – tnw Nov 06 '15 at 16:29
  • You need to read Java basics. Or you will face a lot errors like this. – lewkka Nov 06 '15 at 16:53

1 Answers1

0

I don't really know how and where you need to access these variables or how you intend to use this class but this at least compiles:

class SenderKeyGroupData {

    public static final int MESSAGE = 1;    // final keyword means it cannot be changed
    public static final int SENDER_KEY = 2; // public keyword means it can be accessed by anyone

    static Map MESSAGE_MAP = new HashMap(); // static means this is shared between all instances
    static {
        MESSAGE_MAP.put("name", "message");
        MESSAGE_MAP.put("required", false);
        MESSAGE_MAP.put("type", "7");
    }

    static Map SENDER_KEY_MAP = new HashMap();
    static {
        SENDER_KEY_MAP.put("name", "sender_key");
        SENDER_KEY_MAP.put("required", false);
        SENDER_KEY_MAP.put("type", "SenderKeyGroupMessage");
    }
}
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84