This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Annotations: Why, How & Where?

 
Ranch Hand
Posts: 231
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 21842
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 24940
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21842
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
get schwifty. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!