0

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?

3
  • As i m not getting any error.so please post your error too? Commented Jan 28, 2014 at 7:28
  • Prateek, i edited my question... Commented Jan 28, 2014 at 7:31
  • have a look at my answer Commented Jan 28, 2014 at 7:36

1 Answer 1

3

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"

Sign up to request clarification or add additional context in comments.

1 Comment

My pleasure.I will try to find out some resource for you and provide you with the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.