1

whats the difference between these two declarations?

    public static final int a = 0;
    public static final int b = 1;
    public static final int c = 2;

    @IntDef({a, b, c})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SomeIntDef {
    }

and

    @IntDef({a, b, c})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SomeIntDef {
         int a = 0;
         int b = 1;
         int c = 2;
    }

I mean what is best practice or there any technical differences between these two declarations.

1
  • The former is defining the three possible values of the int; the latter is defining no possible values, but adding 3 fields (with no semantic meaning) with default values to the annotation. Commented Oct 17, 2017 at 13:19

1 Answer 1

0

Yes there is some difference.

  public static final int a = 0;
  public static final int b = 1;
  public static final int c = 2;

  @IntDef({a, b, c})
  @Retention(RetentionPolicy.SOURCE)
  public @interface SomeIntDef {
  }

You are declaring static fields from anchor (parent) class of SomeIntDef. So this is how you reference it

YourParentClass.a

On the other hand,

  @IntDef({a, b, c})
  @Retention(RetentionPolicy.SOURCE)
  public @interface SomeIntDef {
     int a = 0;
     int b = 1;
     int c = 2;
  }

You are going to reference like

ParentClass.SomeIntDef.a

You can also write it as independent class. (Creating a new annotation in another .java file) which makes usage

SomeIntDef.a

These different usages are changing readability.

All of them are acceptable, but the choice is most meaningful one for your design.

Good luck

Emre

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.