What is the practical difference between RetentionPolicy.CLASS and RetentionPolicy.RUNTIME?
It looks like both are recorded into the bytecode and both may be accessed at the run-time anyway.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s not what the javadoc says:
RUNTIME: Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.
CLASS: Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.
In practice, I’m not aware of any use-cases for
CLASS. It would only be useful if you wanted to read the bytecode programmatically, as opposed to via the classloader API, but that’s a very specialised case, and I don’t know why you wouldn’t just useRUNTIME.Ironically,
CLASSis the default behaviour.