Java annotation
Annotation is a form of metadata that can be added to Java source code that is avaible in runtime, in classes methods, variables, parameters and packages, This give us information about the element annotated and allow define who we want manage by the IDE.We can use the default java annotation or just create our annotations. So we can focus this in tow different ways, using the default annotations or just create news.
The most commons annotations:
When we create a Javadoc it’s creates automatically some of then:
@author: The author from the class or function
@version: Code version
@see: it means to see something interesting such as webs other functions etc, to use a link we need use the structure yourlink
@param variable: The explanation of a parameter
@return what return the function.
@since the latest version used
And these are the most used in general programming in java:
@Override This annotation is used when you inherit from other class or abstract class or implement an interface , so “override” means that the function will be over-written
These helps to the developer because he knows immediately that it comes from other parent class and it is over-written.
Maybe it seems confused so let’s se a example with a abstract class:
-We create our abstract class with the abstract function:
And the class where we will test it:
The IDE advertise that we need to implements the parent functions:
When we press this options the IDE generates the function with a autogenerated message:
@Deprecated This annotation is a way to advertise to developers that this function is deprecated and there is another that works better. It happens when we are working in a project with other developers, for example, and the function that you have create is old, and you have other one that works better, so you can’t delete the older function because your partners are working with this so, you advertise them with deprecated and recommend the new one.
We have this example:
And we can create a recommendation in the documentation with {@link} : {@link package.class#function()name}
@SuppressWarnings:
This annotation allows us to advertise to the IDE to not worry about some warnings. This annotation has a parameter type String to declare the warnings.
There is a list of the common warnings:
-all to suppress all warnings.
-cast to suppress about casting.
-null to suppress warnings about null references
-unchecked to suppress warnings about no checked operations
-unused to suppress warnings about no using code
-deprecated to suppress warnings about function deprecated.
When We use the deprecated function at the left side we have the notification.
But when you write this annotation the IDE ignore this warning and don’t notify to you