3

I have enum:

public enum Scope {
    USER, GLOBAL;

    public static final Scope[] TRANSIENT = {};
    public static final Scope[] USER_OVER_GLOBAL = {GLOBAL, USER};
    public static final Scope[] GLOBAL_OVER_USER = {USER, GLOBAL};
}

and annotation:

public @interface Config {
    public Scope[] load() default Scope.GLOBAL_OVER_USER; // Can't use this defval

    public Scope[] save() default Scope.USER;
}

Why I can't use static arrays as default values for annotation's property? My NetBeans 7.3 Beta tell me there is required Scope but found Scope[] - as you can see this not true. Is there a NB's or Java 7 related confusion?

2
  • Are you sure that error is occurring on the line illustrated? Because your code as shown should fail on the line below it. Commented Nov 13, 2012 at 11:51
  • @Perception: this is annotation, so USER is treated as {USER} and it's not a mistype. Problem is with using static hand-made values for annotation's defaults. Commented Nov 13, 2012 at 12:05

1 Answer 1

4

The problem is that the Scope[] GLOBAL_OVER_USER is not all constant. (Yes, the array itself is constant, however you can change the contents of it e.g. GLOBAL_OVER_USER[0] = GLOBAL;.

A workaround is to initiate the array directly in the annotation declaration:

public Scope[] load() default {USER, GLOBAL};
Sign up to request clarification or add additional context in comments.

4 Comments

I know this workaround, but then how to get default value for this annotation property without its instance?
Well, you don't have to. By just adding @Config (or @Config(save=Scope.GLOBAL) ) the annotation's load value will keep it's default value.
Of course, but imagine case when you don't annotate field to imply annotation defaults.
There is a way for getting annotation's defaults.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.