MinecraftShamrock
MinecraftShamrock

Reputation: 3574

Java - Do I have to annotate a package in all classes that are contained by the package?

I receny found out that packages are annotable in Java too. So I really wondered how that would work. Especially how the syntax would work. And that's what i've found out:

 @SomeAnnotation(some="values")
 package x.y;
 class Example {

 }

So I annotated the package x.y with @SomeAnnotation.

Now my question: Do I have to write this annotation over the package declarations in ALL members of this package? And if not, is it possible to put a different annotation to the package in another class?

Upvotes: 1

Views: 160

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136042

The best place to annotate a package is package-info.java file. See JLS 7.4.1

Upvotes: 2

Thihara
Thihara

Reputation: 6969

No you don't have to... If you had to there's no point in having package annotations. (they will be the same as class level annotations)

No it's not possible to quote the JLS

Annotations may be used on package declarations, with the restriction that at most one annotated package declaration is permitted for a given package.

JLS 7.4.1.1

Upvotes: 2

Related Questions