Reputation: 984
Whats the use of fields or classes inside annotations?
public @interface Test {
public String val = "hello"; // WHY??
public static class MyClass {// WHY??
public static void main(String[] args) {
System.out.println(val);
}
}
String value();
}
Upvotes: 0
Views: 118
Reputation: 140318
Fields: because you can define fields in any interface. They're always implicitly public, static and final: in other words, to define constants in the namespace of the interface.
The answer is essentially the same for classes: it's a public, static (but not final) class. it's just a namespace in which to define the class.
To turn the question around: why shouldn't you be able to do this?
There are all sorts of syntactically valid things you can do that you might think are ill-advised. For example:
String String = "String"; String: break String;
is valid, but useless, and confusing. There are 3 different meanings of String
here, 4 if you count the literal. This is far less useful than annotation members, but also allowed.
Sometimes it's effort to stop you doing things: it adds complexity to the language, and the compiler to enforce; and in the effort to prevent the odd piece of madness, you might accidentally remove useful expressivity.
Upvotes: 2