*
The moose likes Beginning Java and the fly likes Annotations: Why, How & Where? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Annotations: Why, How & Where?" Watch "Annotations: Why, How & Where?" New topic
Author

Annotations: Why, How & Where?

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Hi,

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.

Thank you


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
Hello,

Annotation I find useful are @Override and @SupressWarnings.

The @override anotation makes sure that a subclass' method indeed overrides (and not overloads or newly implement) it's superclass' method.

Example:



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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7554
    
  18

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.

Just a tip if you're looking up the docs: There are two 'Annotations' in Java, and the one you're interested in is java.lang.annotation.Annotation, which is an interface (the other one, java.text.Annotation, is a class).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.
- EJB3.x.
- GWT, to inject fields and objects.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Hi all,

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
replaced by:

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
An example of a user-defined annotation.

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Annotations: Why, How & Where?
 
Similar Threads
Why should I use annotations and what are those?
question on ehcache com.googlecode.ehcache-spring-annotations production usage
Annotation dilema
The place where we define Annotations inside the EJB Bean class
Suggestions for custom annotations