417

I haven't touched Java since using JBuilder in the late 90's while at University, so I'm a little out of touch - at any rate I've been working on a small Java project this week, and using Intellij IDEA as my IDE, for a change of pace from my regular .Net development.

I notice it has support for adding interfaces and @interfaces, what is an @interface, and how does it differ from a normal interface?

public interface Test {
}

vs.

public @interface Test {
}

I've done a bit of searching, but couldn't find a great deal of useful info referring to @interface.

mavis
  • 3,100
  • 3
  • 24
  • 32
Bittercoder
  • 11,753
  • 10
  • 58
  • 76

6 Answers6

412

The @ symbol denotes an annotation type definition.

That means it is not really an interface, but rather a new annotation type -- to be used as a function modifier, such as @override.

See this javadocs entry on the subject.

mrkishi
  • 5,602
  • 1
  • 20
  • 18
  • 18
    Great thanks, good to know. So what was the rationale for calling it @interface, rather then say @annotation I wonder.. seems like an unnecessarily overloaded term. – Bittercoder May 27 '09 at 23:46
  • 10
    The tutorial and the JLS allude to an annotation being a special kind of interface. There doesn't appear to be much discussion out there on the subject, but http://javarunner.blogspot.com/2005/01/annotations-in-java-15.html explains that annotations are an implicit extension of the Annotation interface and @ and interface are used to together differentiate from a regular interface. You may also want to read the JSR specification for annotations. – DavidValeri May 28 '09 at 11:41
  • 3
    @Bittercoder the docs do mention: "keyword interface is preceded by the at sign (@) (@ = AT, as in annotation type)". Thats all the rationale I can find w.r.t. naming. – Shaishav Sep 06 '17 at 03:41
  • Actually, Bittercoder's question is quite inevitable, and DavidValeri's link very nicely explains it. So, you can write like this: `@ interface`, not only `@interface`. – starriet Dec 11 '20 at 03:27
  • @Bittercoder Kotlin actually made it better by calling it `annotation` rather than overloading the term `interface`. – EternalObserver Mar 28 '23 at 05:57
153

interface:

In general, an interface exposes a contract without exposing the underlying implementation details. In Object Oriented Programming, interfaces define abstract types that expose behavior, but contain no logic. Implementation is defined by the class or type that implements the interface.

@interface : (Annotation type)

Take the below example, which has a lot of comments:

public class Generation3List extends Generation2List {

   // Author: John Doe
   // Date: 3/17/2002
   // Current revision: 6
   // Last modified: 4/12/2004
   // By: Jane Doe
   // Reviewers: Alice, Bill, Cindy

   // class code goes here

}

Instead of this, you can declare an annotation type

 @interface ClassPreamble {
   String author();
   String date();
   int currentRevision() default 1;
   String lastModified() default "N/A";
   String lastModifiedBy() default "N/A";
   // Note use of array
   String[] reviewers();
}

which can then annotate a class as follows:

@ClassPreamble (
   author = "John Doe",
   date = "3/17/2002",
   currentRevision = 6,
   lastModified = "4/12/2004",
   lastModifiedBy = "Jane Doe",
   // Note array notation
   reviewers = {"Alice", "Bob", "Cindy"}
)
public class Generation3List extends Generation2List {

// class code goes here

}

PS: Many annotations replace comments in code.

Reference: http://docs.oracle.com/javase/tutorial/java/annotations/declaring.html

jsheeran
  • 2,912
  • 2
  • 17
  • 32
mavis
  • 3,100
  • 3
  • 24
  • 32
  • 6
    That was actually useful. I didn't know Java could do this. – Jay Sidri Aug 29 '16 at 05:53
  • The previous answer included this link which is where this information came from. I found it useful to find more information about this topic. https://docs.oracle.com/javase/tutorial/java/annotations/declaring.html – PatS Nov 11 '18 at 01:50
  • 4
    I'm not sure why everyone loves this answer while it gives a very limited use case of the annotations. Annotation in java is used for so many more things than just a replacement for comments. The power of annotations lies in the fact that they can change the way how annotated elements behave or get processed in the runtime – Leonid Usov Aug 20 '20 at 15:33
  • @LeonidUsov can you give one example please. I'm new to Java and JVM based languages. As far as I can see, they seem to implement decorator design pattern. Is that it or am I missing some more functionality that annotations offer? – EternalObserver Mar 28 '23 at 06:02
  • 1
    @EternalObserver No, they are not a decorator design pattern since they don't directly change the way the annotated entities behave in runtime. It's like a piece of metadata that you attach to your code element that can later be used by the compiler to change its behavior or queried in the runtime by the dedicated code. A common example would be `@Serialize-related` set of annotations that change how a given class is packed into a stream or unpacked. – Leonid Usov Mar 28 '23 at 16:57
  • @LeonidUsov Thanks, in Python and JS, `@` symbol is used for decorators. So i was confused. Now I understand that annotations are only for attaching metadata and it's upto the framework on how to make use of the metadata – EternalObserver Mar 28 '23 at 17:20
38

The interface keyword indicates that you are declaring a traditional interface class in Java.
The @interface keyword is used to declare a new annotation type.

See docs.oracle tutorial on annotations for a description of the syntax.
See the JLS if you really want to get into the details of what @interface means.

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
DavidValeri
  • 2,390
  • 15
  • 11
26

interface: defines the contract for a class which implements it

@interface: defines the contract for an annotation

Emmanuel Osimosu
  • 5,625
  • 2
  • 38
  • 39
15

interface in the Java programming language is an abstract type that is used to specify a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword

@interface is used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example:

@interface MyAnnotation {

    String   value();

    String   name();
    int      age();
    String[] newNames();

}

This example defines an annotation called MyAnnotation which has four elements. Notice the @interface keyword. This signals to the Java compiler that this is a Java annotation definition.

Notice that each element is defined similarly to a method definition in an interface. It has a data type and a name. You can use all primitive data types as element data types. You can also use arrays as data type. You cannot use complex objects as data type.

To use the above annotation, you could use code like this:

@MyAnnotation(
    value="123",
    name="Jakob",
    age=37,
    newNames={"Jenkov", "Peterson"}
)
public class MyClass {


}

Reference - http://tutorials.jenkov.com/java/annotations.html

Piyush Singh
  • 223
  • 2
  • 9
0

With @interface you're defining annotations, with interface you're defining interfaces.

From Oracle docs: "Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time."

Annotations usually have 2 major uses: They help to define programming contracts (like @Nonnull annotation), which means they actually do nothing, but serve to programmers and IDES to detect potential error codes, and there are runtime annotations, which are usually used by frameworks for configuration (like the @Service annotation in spring): The core detects clases in yous scope with specific annotations and applies logic to them (any example you can think of, like defining Java Servlers, creating singletons, etc)

Generally speaking, a junior programmer doing some normal basic stuff, will rarely need to define Annotations, but the definitioon and using of interfaces is something basic any new developer should learn if their lenguaje allows it: Good practices USUALLY recommend the usage of interfaces in your code for maintainability.

In my personal experience, I've been defining and using Java interfaces almost since the day I learned Java, but I didn't need to DEFINE an annotation (tagging things with existing annotations is another story) until my 5th-6th year working proffesionally with Java, and it was a very specific project, a code autro-generator, something like Lombok but much more specific for a client.

I guess it depends on the nature of your work, but to day, I can't count how many interfaces I've defined, but annotations could be counted with the fingers of both hands. I beet there are good chances that a Java developer can start and end their entire career without defining a single interface (unless you use swagger's annotation based documentation generation xD)

DGoiko
  • 346
  • 2
  • 19