#where is @interface defined?

1 messages · Page 1 of 1 (latest)

royal sleet
#

Context: I am a professional NodeJS dev who likes to explore other languages in its spare time.

I'd like to understand annotations, my problem is that all tutorials I've seen introduce 10 built-in annotations:

@Override
@Deprecated
@SuppressWarnings
@Retention
@Documented
@Target
@Inherited
@SafeVarargs
@FunctionalInterface
@Repeatable

But then all the examples start with @interface. If @interface is not one of these 10 basic ones, where is it defined?

I grepped the official java spec and it seems to only use @interface in code examples too. Also the latest spec doesn't list @documented among the built-in, did @documented get @deprecated ? 🤣

supple jettyBOT
#

<@&987246964494204979> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Context: I am a professional NodeJS dev who likes to explore other languages in its spare time. I'd like to understand annotations, my problem is that all tutorials I've seen introduce 10 built-in annotations: ``` @Override @Deprecated @SuppressWarnings

come across talk about annotations, but never mention where the @interface keyword is defined. Can someone explain where this keyword is defined in Java?

The @interface keyword in Java is used to define custom annotations. It is not defined in a specific class or package, but rather it is a reserved keyword within the Java language itself.

When you use the @interface keyword, you are essentially creating a new annotation type that can be used to annotate classes, methods, fields, and other elements in your code.

To define a custom annotation using the @interface keyword, you would typically create an interface with the @interface annotation before the interface keyword.

For example:

@interface MyAnnotation {
    String value();
}

In this example, we have defined a custom annotation called MyAnnotation that has one element called value of type String.

You can then use this custom annotation in your code like any other built-in Java annotation.

I hope this clarifies where the @interface keyword is defined in Java and how it can be used to create custom annotations. Let me know if you have any other questions!

grizzled parrot
royal sleet
#

I know it's a keyword but I thought it was also an annotation

grizzled parrot
#

it's not

royal sleet
#

Can any keyword be used as annotation?

grizzled parrot
#

ah no

#

keywords can't be used as identifiers

royal sleet
#

I thought terms that started with @ were annotations

#

Is interface the only exception?

grizzled parrot
#

you use @ only at two places

#

either when you want to declare an annotation with interface so @interface
or when you want to apply an annotation

#

You can still do things like Override.class

royal sleet
#

So if I see a term that starts with @ it's either one of the basic annotations, or the keyword @interface, or a custom annotation defined in userland, right?

supple jettyBOT
#
Alathreon's result

Snippets

Snippet 23, VALID

@interface Foo {}```
### Snippet 24, VALID
```java

println(Foo.class + " " + Foo.class.isAnnotation());```
### Snippet 25, VALID
```java

@Foo void bar() {}```
## System out

interface REPL.$JShell$23$Foo true

grizzled parrot
royal sleet
#

I can't find it

grizzled parrot
royal sleet