Java Magazine, March/April 2016
ORACLE COM JAVAMAGAZINE MARCH APRIL 2016 36 inside java is the target of the metadata is much safer starting with the fact that now you are no longer duplicating the name of that method a violation of the DRY principle Dont Repeat Yourself Here are some other good examples of annotation uses Code correctness Annotations such as @ Nullable @ Deprecated and @ Override add important semantic information to methods and fields that the compiler can enforce Test methods Before TestNG and annotations came along JUnit was using reflection to indicate that a method was a test method which required specific naming conventions With an annotation its no longer necessary to use naming conventions Persistence for example Hibernate You can annotate fields and methods in order to tie them to data stored in the database Dependency injection Classes that need to be injected can be annotated as such along with fields and parameters Graphical toolkits As an example Android describes graphical layouts using XML with annotations you can now directly tie graphical elements text views buttons and so on to the field that holds their reference Note that all these examples share the same characteristic tying information to Java elements In contrast here are a few examples of metadata that are not good fits for Java annotations Deployment information such as host names ports passwords and other authentication details Connection pools informing your application how to connect to a database Parameters describing how an application should be launched or what kind of information is accessible at runtime Important Annotations Annotations are pretty much unavoidable in modern Java and plenty of libraries provide their own but there are a few that stand out and that you should be using regularly @ Nullable and @ Nonnull javax annotation These annotations can be placed on fields and method parameters and they indicate whether these variables can be null They are extremely useful and a lot of tools on top of the Java compiler javac recognize them starting with the major IDEs You should use them at every opportunity You will quickly notice the number of null pointer exceptions in your codebase sharply decreasing @ Override You are probably already familiar with this annotation because it has been mandatory since Java 6 and for good reason This annotation must be placed on any method overriding a method from a parent interface or class It prevents you from accidentally overriding a method or conversely from thinking you overrode such a method but did not because of a typo @ FunctionalInterface This is a new addition to Java 8 It makes sure that the interface so annotated is indeed a functional interface that is an interface with exactly one abstract method The idea behind this annotation is that if one day you or someone on your team accidentally adds an abstract method to that interface the compiler will issue an error @ SuppressWarnings This annotation is self explanatory Warnings are usually extremely useful and you should never turn them off globally However its occasionally useful to turn them of for specific statements or expressions when you know that your code is safe but the compiler doesnt Writing an Annotation Lets take a look at a popular annotation @ Test Here is its definition
You must have JavaScript enabled to view digital editions.