Part 1. I've written very briefly about annotations of the SOURCE and CLASS type. This is worth reading, so as to avoid getting lost in the second part and to expand your "misunderstanding" a little =) I promise there will definitely be at least one word that you know!
The first time I saw annotations in the tasks here I somehow didn't pay much attention to them. There's @Override here and there, but IDEA adds that, so I figured it has to be that way. Over time, I realized that everything is much deeper.
As you study, annotations may seem somewhat useless but necessary. You don't know why they exist or what they do. You've read a couple of articles that said, "it's so great that we have annotations now, everything has become so simple." But I didn't know how things were before, and I didn't understand that things are easier now. Now I do know and want to share a little.
There are 3 types of (RetentionPolicy) annotations:
Native — I never seen this and never used it. I think this is a rather rare annotation, because it is used when you need to run code in another "native" language. I tried and failed to find a clear mention of it.
SuppressWarnings — This annotation is often used like this: @SuppressWarnings("unchecked"). It is used to suppress warnings that you are already aware of. The previous example suppresses warnings about unchecked type conversions. Again, this is the only usage that I've encountered.
Generated — I'm running into this annotation right now due to an assignment where I have to generate classes from XSD files. These 3 annotations are quite specific and are most likely uninteresting to you at present. I will describe the last one.
Override — You use it constantly and it does something very useful. When overriding a method, it is easy to make a mistake without IDEA's help. Whether typos or simple errors, mistakes happen. This annotation will make sure that the method in the parent class matches our (annotated) method. This ensures that the method will be overridden rather than added. When refactoring code, the parent method may be removed or changed. Again, this annotation will indicate an error. Without it, our method would simply be added.
Boring? I would say yes. There's not much helpful to glean from this article. Almost everything (90%) here describes something that you will never use or only very rarely. The remaining 10% is saying hello to and describing the @Override annotation, which at first glance is useless. That said, I think in the second part of the article will be more interesting. There will will discuss RUNTIME annotations — they interact with the code during execution and do black magic.
Annotations. Part 2. Lombok
- SOURCE— Annotations for the compiler
- CLASS— Information from the annotation will be written in bytecode but not available at runtime. They say that the standard library has many annotations of this type, which is now retained for backward compatibility. This is used for very specific tasks.
- Q&A on StackOverflow
- RUNTIME — These annotations are the most popular. They are used while the code is being executed.
- java/lang/annotation/Native.class;
- java/lang/SuppressWarnings.class
- javax/annotation/Generated.class
- java/lang/Override.class
- com/sun/istack/internal/Nullable.class
- com/sun/istack/internal/NotNull.class
- com/sun/istack/internal/Interned.class
- Native — A variable with this annotation may refer to native code;
- SuppressWarnings — This annotation suppresses various compiler warnings;
- Generated — This annotation marks source code that was generated;
- Override — This annotation checks method overrides.
GO TO FULL VERSION