I have tried reading docs and other common resources in order to understand about annotations and their usage, but am unable to grasp the proper usage of them. I am not understanding annotations clearly. Could you help please.
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Rajdeep Biswas wrote:I have tried reading docs and other common resources in order to understand about annotations and their usage, but am unable to grasp the proper usage of them.
I have to admit, the docs are remarkably unhelpful when it comes to annotations - possibly the nature of the beast.
My advice: Start with the tutorials. That at least should give you some basis to start from. They're definitely worth getting to know, because you can do some nifty things with them, but it has to be said that that you can live perfectly happily without; after all, they took 5 full releases to appear.
Like everything, the best thing is to start slowly: get used to the ones that the compiler uses. After a while, maybe try writing one of your own and test it out.
Michael Krimgen wrote:The annotation @SupressWarnings can be used to tell the compiler not to show certain warnings. You can e.g. use a deprecated method without
getting loads of compiler warnings.
But you should use @SuppressWarnings as little as possible. A compiler warning is usually there for a reason, and should only be suppressed after you've given it a lot of thought. Deprecated methods for instance should be avoided unless you really, really have to use it. Which you probably don't.
Another rule of thumb about @SuppressWarnings: keep its scope as small as possible. Never* apply it to a full class. If possible, apply it to local variables only. For instance, to get files from the clipboard:
* Well, almost never. @SuppressWarnings("serial") is the only one I might consider applying to an entire class, although I usually just add a serialVersionUID field.
As for the question "Why", I have to say that the Oracle tutorial doesn't really cover that very well. It gives only one example of an annotation, apart from the built-in ones, and that's only for documentation.
As for the question "How", the tutorial hardly covers that at all. It says that annotations can be made available at runtime, but it doesn't provide any examples of that at all. It's almost as if a whole page of the tutorial has been left out.
So... yeah, the question about what would be a good application for annotations and how could we implement them goes pretty much unanswered by the tutorial. And when you google up other tutorials, you get pretty much the same puff pieces, all written about 2005 when Java 5 first came out. However I did find a Jenkov tutorial which shows how to use reflection to access runtime annotations. It's not a great tutorial (he didn't bother to figure out a real-life application, he just went with "MyAnnotation") but at least it's something. I couldn't find any tutorials which were based on a real-life example.
If you want to see some annotations actually become useful, I suggest you look at one of the following:
- Servlets using the Servlet 3.0 spec - WebServlet and WebListener can partly replace web.xml.
- JPA - your entire datamodel is annotated.
- GWT, to inject fields and objects.
I am glad to have received really helpful replies. I am starting learning annotations and applying them as well. I will continnue this thread in case I have some query. So far I have understood @Override. I did not grasp the following, whatsoever
and the comments inside a class
// Author: John Doe
// Date: 3/17/2002
I was once using a home-grown generator (stemming form pre-JPA times) essentially written by a colleague.
It perused the given stored packages of the data base and generated wrapping Java stuff
in order to make easier the calling of the stored procedures and to represent the set of their return values.
The generated stuff was very useful in a two-tiered application.
However the need arose to introduce a third tier: to hide the database calls behind a web service.
I wanted to generate the web service based on the above described generated stuff,
and those classes contained all the necessary information - except the original parameter names,
which are nicer then generated names like "p0" and "p1" etc.
So I decided to include this piece of information in the form of user-defined annotations,
which were defined to be retained in the class.