Government Security
Network Security Resources

Jump to content


Java Annotation

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

#1 Juza



  • 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.*;

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: " +;
		System.out.println("Version: " + annotation.description());  	


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

Hope you have learn something.
Go to
Follow me

The true beginning of our end.

Also tagged with one or more of these keywords: java