[Java] java_annotation_memo_20200807

2 minute read

Annotation

** 1. What is an annotation? **

Annotation means that you can write an instruction in the source code that “this part does not need to issue a warning”.

Annotation is a description that starts with “@” and is described as it is in the source code, not in the comment.
Let’s actually check what it looks like by taking the three annotations prepared as standard below as an example.

> Since it has already been described in a different article, I will limit it to this much explanation.

** 2. Code using annotations **

コメント 2020-08-07 105055.png

** 3. @SuppressWarning –Suppress warnings **

Instructing classes, methods, fields, etc. not to give certain types of warnings
** @SuppressWarning annotation **.

This annotation specifies “the type of warning you want to suppress or control” as a parameter.
For example, in “2. Code using annotations”,
@SuppressWarning (“Serial”) will not warn you that you have not declared a serialVersionUID.
In addition, this annotation has the parameters shown in Table 1 below.

** Typical parameters that can be specified in Table 1 @SuppressWarning **

Comment 2020-08-07 115055.png

** 4. @Override –Declaration of Override **

If you add ** @Override annotation ** to the beginning of the method declaration, that method will be the method with the same name in the parent class.
You can explicitly declare that you want to override it.

Why do you need something like this?

If you think you’ve overridden, but you’re not, you’ll notice it

For example, with the intention of overriding the method transfer () declared in the parent class,
Let’s say you have declared the misspelled method transfer () in your child class.
This is not an override, so no matter how many times you call transfer (), the parent class transfer () will
It works, and the child class transfer () does not work.
    
Therefore, there is a problem that it is difficult to identify the cause that “the method can be called and works for the time being, but the operation content is strange”.
It will occur.
Therefore, add @Override annotation at the beginning of the method declaration that is supposed to be overridden.
The compiler will warn you if you do

** 5. @Deprecated –Deprecated Declaration **

If you add ** @ Deprecated annotation ** to the beginning of declarations of classes, fields, methods, etc.
You can tell the compiler that it is deprecated.
For example, you don’t want the older class to be used as a result of modifying one class to create a better one.
Describe at times.

The description continued for a long time, but since it is a memo for myself in the future, I think that there are some mistakes in reading it.
Please point out any mistakes.