- The Java Workshop
- David Cuartielles Andreas G?ransson Eric Foster Johnson
- 919字
- 2021-06-11 13:05:22
Annotations
Annotations are a special type of metadata that can be added to your code to inform the compiler about relevant aspects of it. Annotations can be used during the declaration of classes, fields, methods, variables, and parameters. One interesting aspect of annotations is that they remain visible inside classes, indicating whether a method is an override to a different one from a parent class, for example.
Annotations are declared using the @ symbol followed by the annotation's name. There are some built-in annotations, but it is also possible to declare your own. At this point, it is important to focus on some of the built-in ones, as it will help you to understand some of the concepts presented so far in this chapter
The most relevant built-in annotations are @Override, @Deprecated, and @SuppressWarnings. These three commands inform the compiler about different aspects of the code or the process of producing it.
@Override is used to indicate that a method defined in a child class is an override of another one in a parent class. It will check whether the parent class has a method named the same as the one in the child class and will provoke a compilation error if it doesn't exist. The use of this annotation is displayed in the following example, which builds on the code we showcased earlier in the chapter about the Tablet class extending the Computer class.
class Computer {
public void whatIsIt() {
System.out.println( "it is a PC");
}
}
class Tablet extends Computer {
@Override
public void whatIsIt() {
System.out.println( "it is a tablet");
}
}
class Example12 {
public static void main(String[] args) {
Tablet myTab = new Tablet();
myTab.whatIsIt();
}
}
@Deprecated indicates that the method is about to become obsolete. This typically means that it will be removed in a future version of the class. As Java is a living language, it is common for core classes to be revised and new methods to be produced, and for the functionality of others to cease being relevant and get deprecated. The following example revisits the previous code listing, if the maintainer of the Computer class has decided to rename the whatIsIt() method getDeviceType().
Example13.java
1 class Computer {
2 @Deprecated
3 public void whatIsIt() {
4 System.out.println( "it is a PC");
5 }
6
7 public void getDeviceType() {
8 System.out.println( "it is a PC");
9 }
10 }
11
12 class Tablet extends Computer {
13 @Override
14 public void whatIsIt() {
15 System.out.println( "it is a tablet");
16 }
17 }
Calling the compilation of the previous example will issue a warning about the fact that the whatIsIt() method will soon be no longer used. This should help developers plan their programs, as they'll know that some methods may disappear in the future:
Warning:(13, 17) java: whatIsIt() in Computer has been deprecated
@SuppressWarnings makes the compiler hide the possible warnings that will be defined in the annotation's parameters. It should be mentioned that annotations can have parameters such as overrides, deprecation, divzero, and all. There are more types of warnings that can be hidden, but it is too early to introduce them. While we are not going to go deeper into this concept at this point, you can see an example of this in the following code listing.
Example14.java
1 class Computer {
2 @Deprecated
3 public void whatIsIt() {
4 System.out.println( "it is a PC");
5 }
6
7 public void getDeviceType() {
8 System.out.println( "it is a PC");
9 }
10 }
11
12 @SuppressWarnings("deprecation")
13 class Tablet extends Computer {
14 @Override
15 public void whatIsIt() {
16 System.out.println( "it is a tablet");
17 }
18 }
When calling the compilation of the latest example, you will see a difference in comparison to the previous one, as the compilation of this one will not produce any warnings regarding the deprecation of the whatIsIt() method.
Note
You should be careful when using @SuppressWarnings as it can hide risks derived from potential malfunctions of your code. Especially avoid using @SuppressWarnings("all"), as it could mask warnings that could be producing runtime errors in other parts of your code.
- AngularJS Testing Cookbook
- C語言程序設(shè)計(jì)
- Spring Boot Cookbook
- Python期貨量化交易實(shí)戰(zhàn)
- JQuery風(fēng)暴:完美用戶體驗(yàn)
- C++程序設(shè)計(jì)
- Learning Unreal Engine Game Development
- 量子計(jì)算機(jī)編程:從入門到實(shí)踐
- 深度學(xué)習(xí):基于Python語言和TensorFlow平臺(tái)(視頻講解版)
- 小小的Python編程故事
- Building RESTful Web Services with PHP 7
- Mastering Chef Provisioning
- Flink大數(shù)據(jù)分析實(shí)戰(zhàn)
- LLVM Essentials
- Linux C編程從入門到精通(“十二五”國(guó)家重點(diǎn)圖書出版規(guī)劃項(xiàng)目)