How to pass in a @IntDef annotation type as function parameter

Viewed 91

I have a annotation type defined as

@IntDef({
  MOOD.HAPPY,
  MOOD.SAD,
})
public @interface MOOD {
  int HAPPY = 0,
  int SAD = 1,
}

Now I have a function defined as

SomeFunctionName(MOOD mood) {
  doingSomething...
}

I don't know how can I pass a MOOD value to this function. Compiler always complains type couldn't convert For example, I tried

SomeFunctionName(MOOD.HAPPY)
SomeFunctionName(0)
1 Answers

Your function SomeFunctionName is declared to take a MOOD, but you are trying to pass an integer.

This compiles fine:

@interface MOOD1 {
  int HAPPY = 0;
  int SAD = 1;
}

interface MOOD2 {
  int HAPPY = 0;
  int SAD = 1;
}

public class SO67518698 {
  void m1(MOOD1 mood) {}
  void m2(MOOD2 mood) {}
  void m3(int x) {}

  void client() {
    m3(MOOD1.HAPPY);
    m3(0);
}

You should also think about whether you are trying to define an interface (use interface) or an annotation (use @interface).

Related