Government Security
Network Security Resources

Jump to content

Photo

Java Annotation

- - - - - java
  • Please log in to reply
No replies to this topic

#1 Juza

Juza

    Specialist

  • Sergeant Major
  • 149 posts

Posted 31 October 2010 - 02:03 PM

One of the coolest things that java permits to do, is add annotations on classes and methods.
This feature can extremely useful, for example build your how unit of tests or something else.

The interface is here you define or annotation.

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
	String name();
	String description();
}


And in your class.

@Annotation(name="Something", description="This is a annotation")
public class Main {

	public static void main(String[] args) throws ClassNotFoundException {
		Class<?> klass = Class.forName("Main");
		Annotation annotation = (Annotation) klass.getAnnotation(Annotation.class);
			
		System.out.println("Annotation " + klass);
		System.out.println("name: " + annotation.name());
		System.out.println("Version: " + annotation.description());  	

	}
	
}

Results:
Annotation class Main
name: Something
Version: This is a annotation

Hope you have learn something.
Go to iamjuza.blogspot.com
Follow me twitter.com/iamjuza

The true beginning of our end.






Also tagged with one or more of these keywords: java