CodeGym/Java Blog/Java Developer/Annotations. Part 1 — a little boring
Victor Sergeev
Level 40
St. Petersburg

Annotations. Part 1 — a little boring

Published in the Java Developer group
members
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! Annotations. Part 1 — a little boring - 1 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:
  • 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.
The introduction took up part of the article, so I will write here about SOURCE and CLASS annotations. These are the annotations I could find (thanks to Task 3607). I won't address runtime annotations — there are too many of them and they are not the topic of this article. SOURCE:
  • java/lang/annotation/Native.class;
  • java/lang/SuppressWarnings.class
  • javax/annotation/Generated.class
  • java/lang/Override.class
CLASS: I don't know why CLASS annotations are necessary. The documentation for the existing annotations is nowhere to be found, so I think you can just leave this baggage behind. But if you find it, please share. SOURCE annotations:
  1. Native — A variable with this annotation may refer to native code;
  2. SuppressWarnings — This annotation suppresses various compiler warnings;
  3. Generated — This annotation marks source code that was generated;
  4. Override — This annotation checks method overrides.
For more information:

@Native

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

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

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

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
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet