How to set String Array in Java Annotation

Jakob Abfalter picture Jakob Abfalter · Dec 17, 2013 · Viewed 63.4k times · Source

I have declared a annotation like this:

public @interface CustomAnnot 
{
    String[] author() default "me";
    String description() default "";
}

A valid Annotation therefore would be

@CustomAnnot(author="author1", description="test")

What I can't figure out is, how to set more than one author, since author() has return String[] this should be possible.

@CustomAnnot(author="author1","autor2", description="test")

doesn't work!

Answer

Kai Sternad picture Kai Sternad · Dec 17, 2013

Do it like this:

public @interface CustomAnnot {

    String[] author() default "me";
    String description() default "";

}

And your annotation:

    @CustomAnnot(author={"author1","author2"}, description="test")