Why is @interface used to define annotations?

I use annotations in Java or some time as an end user, but recently I decided to look at creating my own types of annotations, and I find the syntax for defining annotations in Java with @interface very strange. My question is why Java uses @interface to define annotations instead of entering a new keyword, as it did for enums? Is there any advantage of the @interface syntax that I am missing?

I get attached to understand the design considerations that annotation designers went through. I am sure that they must have played with the idea of โ€‹โ€‹introducing a new keyword for defining annotations.

@interface has too many restrictions, for example, you cannot use the extension, there are certain types that you cannot use when defining an annotation member, for example Date. I find limitations on what can go into @interface to not be obvious, and it just seems to hack me.

+7
source share
3 answers

I do not know the exact considerations for this particular case, but in general:

Introducing a new keyword into the language violates the compatibility of all existing source code that is used to use that particular keyword as an identifier.

Existing source code cannot be compiled with the new version of the compiler until it is modified to avoid the keyword. This cannot be overcome (as the example of enum demonstrated), but it is inconvenient and forces many people to do extra work. Java developers usually tried to introduce new language features without compromising source compatibility.

In the case of the enum you mentioned, I think they decided that it (a) is a common keyword in other C-style languages, (b) is usually used only as an identifier for a local area in existing code, and thus it is easy to refactor and (c) without any reasonable alternatives. They decided that the benefits outweighed the costs. For the annotation case, they apparently decided differently.

As an aside, you might be interested in viewing the Josh Bloch Effective API Design , which addresses many of these considerations.

+5
source

Annotation ads are usually very disgusting to watch.

They probably thought that only a few (experts) would declare and process annotations; most programmers would simply use annotations developed by experts. Therefore, they did not think too much to decorate annotation announcements. enum supposed to be used by most programmers in everyday work, so the syntax should be concise.

But now more and more frameworks like Guice / CDI require / encourage application programmers to announce their own annotations. And many programmers feel bold enough to design and process their own annotations. The issue of messy syntax for annotation declarations is becoming more prominent.

+1
source

Obviously, the developers did not want to add a keyword. Not that you do it easily, as it invalidates the existing correct programs. The Cobol-9x team added dozens, if not hundreds of keywords, and you should have heard screams. Some companies talked about filing a lawsuit with a standardization body.

0
source

All Articles