Question

I have written below the Custom Annotation.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

    String value();

}

and am using the annotation as below.

@MyAnnotation("someValue")
public void someMethod(){


}

above code is working fine without any issues. But in the annotation class, value() method name i have to reanme. Can i do as below?

@Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {

        String name();

    }

I tried doing but eclipse is giving the compilation error.

- The attribute value is undefined for the annotation type 
     MyAnnotation
    - The annotation @MyAnnotation must define the attribute 
     name

Any reason?

Was it helpful?

Solution

Use it like this :

@MyAnnotation(name="someValue")
public void someMethod(){


}

because by default annotation has value method so if you specify like this

@MyAnnotation("someValue")
    public void someMethod(){


    }

it will by default take it as value="someValue"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top