+ 1
How to use annotation in java?
Does anyone know a video or any source from which I can understand the concept of annotations in java on a deeper level? They're causing me quite the confusion since I started learning.
3 Respuestas
+ 2
Annotations in Java are like notes you can add to your code to give extra information. They don't change how the code runs but can help with things like documentation, code analysis, and even influencing how the code gets compiled or run. For a deeper dive into how annotations work and how you can use them, check out this website: https://www.javatpoint.com/java-annotation
+ 2
Mainly @SuppressWarnings and @SafeVarargs controls warnings while code runs, also @Override can help to avoid inheritance mistake.
+ 1
There are some annotations that change how code runs. For instance, unit testing with JUnit, there are annotations such as @before and @after that tell JUnit to execute the code either before or after execution of the test suite.
@Override is the most common one, and one that you should always use when the method is overriding the behaviour of the parent class. The annotation will throw an error if the method does not override any functionality. In other words, @Override guarantees something is overridden.
There's a couple others that haven't been mentioned that you can find here : https://www.baeldung.com/java-default-annotations
@functionalinterface enforces that the interface has only one abstract method. This could be useful when working in a larger team.
If you're building a library / tools, the @deprecated annotation could be useful to tell others using your work that something is no longer supported.
An annotation processor manages these, typically at compile time.