• Post Reply Bookmark Topic Watch Topic
  • New Topic

suppresswarning annotations real implementation  RSS feed

 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

i was wondering how the compiler recognize the element of the annotations like @SuppressWarning("unused").
and one more thing i read annotation elements can be set values while decleration of annotations, what is the purpose of those values anyway,i mean , never we are able to use as we use the variable.

i know everything has a purpose but i couldn't get it.
can any one explain the later with example.

thank you
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically annotations are level of meta data that is available to use, not mandatory, to let the compiler know that you know what you are doing. I don't know if you've seen the Java tutorial on annotations, but here is the link. It's an excellent reference: Annotations in Java

A very simple example is: before annotations the compiler had to assume that you knew what you were doing if you overrode a method, such as, paint. Now with annotations most of the IDE's will give you a little added warning via a tag that says--hey, you're doing an override--and you can then do the annotation for override "@Override" and both you and the compiler know that you actually meant to override that paint method instead of it being an accident cause by a poor naming scheme in your program.
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the reply morgan.

I don't know if you've seen the Java tutorial on annotations, but here is the link. It's an excellent reference: Annotations in Java


that's first place before i go elsewher for clarification.


A very simple example is: before annotations the compiler had to assume that you knew what you were doing if you overrode a method, such as, paint. Now with annotations most of the IDE's will give you a little added warning via a tag that says--hey, you're doing an override--and you can then do the annotation for override "@Override" and both you and the compiler know that you actually meant to override that paint method instead of it being an accident cause by a poor naming scheme in your program


annotations like @override are undestandable but what about annotations with elements having values? what it actually buys you.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember these are basically metadata extensions on a lot of it, so you can gain in better meta data which equates to making things more readable or understandable to the next guy when they have to look at your code or in the long run that next guy could be you and it could help you in getting your head back around what you were doing when you wrote that segment of your program. Also using annotations like "@SuppressWarnings("deprecation")" you can say, OK, I know that this condition is going to happen and I want to ignore that or possibly you want to enhance it depending on the argument you give to a parameterized annotation.

s sivaraman wrote:annotations like @override are undestandable but what about annotations with elements having values? what it actually buys you.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More concrete example. You are creating a List<T> class and you want to create an array to hold the elements:-The compiler doesn't seem to like that. So what are you going to do? You want to have a specific type of array, so you won't like this:-You can however use a cast:-The compiler will happily believe you, but produces a warning about unchecked casts.
Note: ArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Now, you are absolutely sure that nothing can go wrong with that cast, so we have a new version:-Only use the suppress warnings annotation when you are absolutely sure the code is correct.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!