0

just started learning android/java and i noticed many appearances of '@' in the code. @Override i can find using google, but the rest of it is a mystery. Eg while using ButterKnife library we declare variables like this:

@BindView(R.id.note1) TextView importantNote;

Does '@' have any special functionality, or is it just a regular part of method BindView() name? Since I couldnt find anything specific about '@', I came up with an idea it could be some kind of convention. Am I right? If so, what is the convention?

Leo
  • 2,061
  • 4
  • 30
  • 58

2 Answers2

2

@ is declaring an Annotation. When compiling the code, an Annotation Processor is scanning the code for those annotations and can generate code as you need (for example, the BindView annotation generates the findViewById code needed to bind a view to a variable) or as additional compiler instruction (for example, Override makes sure that the variable actually overrides something or else you get a compiler error).

For more on how to create your own annotations (and how annotation processors work) read here: https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Processor.html

techfly
  • 1,826
  • 3
  • 25
  • 31
2

Check out https://docs.oracle.com/javase/tutorial/java/annotations/

These are "annotations".

For example:

@Override
protected void overridenMethod() {
}

Annotations can have arguments, like this:

@SuppressWarnings("unused")

The example of @BindView is an inline annotation. Inline annotations are very uncommon, other examples of inline annotations are very specific @SuppressLint annotations.

You can make your own annotations, however, if you are making annotations for compiler warnings, error etc., in this case the IDE must detect the annotation. For example, SuppressLint will work only if IDE supports the annotation, otherwise, annotation won't have any effect.