File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Enum RetentionPolicy Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Enum RetentionPolicy" Watch "Enum RetentionPolicy" New topic
Author

Enum RetentionPolicy

Dave Wagner
Greenhorn

Joined: Jul 05, 2007
Posts: 8
Can somebody explain the real-life need for @Retention(RetentionPolicy.RUNTIME) is? I looked at the javadoc which explains what it does but not why I will ever need it.

Thanks
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3026
    
    4

The RetentionPolicy pertains to Annotations, which provides a sort of meta-data about your code. To learn more about Annotations read The Annotations Tutorial.


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

With @Retention(RetentionPolicy.RUNTIME), the annotation is preserved during runtime, and can be retrieved using reflection. You could use this for instance for testing purposes. Suppose you have an annotation called @Test that indicates a method is a test method. Then you could write an automatic testing tool:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35224
    
    7
Quite a few libraries now work by looking at annotations - JUnit 4 (which is somewhat like what Rob mentions), but also APIs like JAX-WS and JAX-RS, and frameworks like Stripes. It cuts down on the number of config files that were traditionally used to keep this kind of information.

RetentionPolicy.SOUCE is useful for tools handling just the source code (like the javac compiler); @Override is an example of that.

What I don't understand is why RetentionPolicy.CLASS is the default; it seems less useful than the other two options.


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum RetentionPolicy
 
Similar Threads
No comments on this???
about toString() method in K&B's book
Yes, I need a Head First EJB 3.0 book!
Where can i find the original source of crc.
1 year experience job wanted in USA,CANADA