• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use Annotations in Java v5.0  RSS feed

 
Subrahmanyam Baratam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can any one tell the advantage of using annotaions and kindly provide a simple example how to use annotations in Java v5.0.

Thanks in advance,
Subrahmanyam.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you happen to check this tutorial.

Annotations are a good way of putting metadata in the code that tools can use to do some useful processing. eg: EJB3 uses annotations to generate deployment descriptors.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heya, welcome to the JavaRanch.

To help in your answer though, I use @Override a lot. You place that annotation above a method that is supposed to be overriding a method. The advantage is that the second you look at the annotation you can see that the method IS overriding a method and if the method does NOT override a method the compiler will yell at you. Say you have a parent class that has many many child classes that override its methods. Now lets say you change the method signature in the parent class. If you are not using annotations you just created a messy situation that produces no errors or warnings. All of the child classes that should be overriding that method now just have a different named method instead. By using @Override you produce more compile-time checks which can save you hours/days of headache without it.

EDIT: I am retarded I hope no one saw my original post!
[ November 25, 2008: Message edited by: Brian Legg ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do a search; a similar query came up about 2 weeks ago.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to Brian's post, @Override can prevent some nasty errors.

Let's say you want to override a method (hashCode() is a good example). You start out but make a typo. The @Override will warn you about the typo:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope, Brian, that you don't know anybody who would let a class loose on an unsuspecting world and then change its public interface like that.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
To add to Brian's post, @Override can prevent some nasty errors.
. . . You start out but make a typo.


Remember it's @Override not @override nor @Overrides.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!