Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Java annotations

 
Ranch Hand
Posts: 115
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls anyone help me out , how to use annotations... and what is the purpose of it in real time apps...

Thanks in advance,
Devisri.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotations are a way of marking code, not to influence how it behaves, but to influence how tools that read the file treat it. See this.

(and by the way they are part of the Core API, so this might be better placed in the beginners forum)
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Sturrock:
Annotations are a way of marking code, not to influence how it behaves, but to influence how tools that read the file treat it.



I don't quite agree with this. The line between the code that contains annotations and the code that processes them can be very fine. After all, any class is capable of reading and processing its own annotations at runtime. That may not be what Sun envisaged annotations being used for, of course, nor good development practice, but it's possible.

Here's an example of a class that reads its own annotations at runtime. Note the RetentionPolicy.RUNTIME setting.
[ June 14, 2007: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Paul Sturrock:
Annotations are a way of marking code, not to influence how it behaves, but to influence how tools that read the file treat it.



If thats true, then why did sun use Annotations heavily for ejb 3. As far as I get,I see EJB 3 uses annotations to influence the code.

But anyways, are using annotation at run time a bad practice?
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shashank Tilwalli:
As far as I get,I see EJB 3 uses annotations to influence the code.


As far as I get,I see EJB 3 uses annotations to influence the deployment.I guess.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Sturrock:
(and by the way they are part of the Core API, so this might be better placed in the beginners forum)



Moving this to Java In General (Beginners) forum.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
with all due respect, i'm going to move this again

Given the interesting discussion that's occuring, this seems to me to be an intermediate level question.
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What it boils down to is that annotations are class metadata. As such they can be used for whatever purpose someone can think of. Of course they provide more value if they are standardized, so that annotation-processing code knows what to do if they are encountered.

But there's nothing in them that would prevent someone from inventing new ones for use solely in his own code (or solely for use within a single company).

Note that above I used the deliberately vague term "annotation-processing code". This could be tools like apt -which is used for JAX-WS web services-, or javac (think @Override), or 3rd-party libraries like FindBugs (see here).

Or a class could process its own annotations, like in the example I linked to above. That class doesn't do anything interesting with those annotations - it's just meant to demonstrate that it's possible.

Another interesting example is the web app framework Stripes, which uses annotations for configuration purposes at runtime. It thus needs no configuration files, like Struts does.
[ July 07, 2007: Message edited by: Ulf Dittmer ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic