0

I have one interface on the legacy project which defines as follows:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Statistic {

    String name();

}

.So here @interface are followed with two annotations i.e.(@Retention & @Target). Is anybody can explain how this interface works and what would be the good place to use it. I am quite new with these one.

Moreover, I can see on the project code somewhere using this interface with annotating like below:

@Statistic(name="DDB/received") private int docReceived = 0;

What does this really meant? Can anybody elaborate clearly? Thanks

puru
  • 266
  • 5
  • 13
  • possible duplicate of [How and where are Annotations used in Java?](http://stackoverflow.com/questions/1372876/how-and-where-are-annotations-used-in-java) – Andrey Atapin Dec 31 '13 at 05:48

2 Answers2

0

Check this..

Basic Annotation tutorial , Custom Annotations , Why annotations , When and Where

and finally Java Doc

Community
  • 1
  • 1
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
0

This interface defines an annotation that is retained for use at runtime. This would allow you to test whether a field or method was marked with it to perform some action upon it. The annotation can also have metadata associated with it for use by the processor. In this case, you can provide a name.

The below illustrates two cases where this annotation can be used:

ElementType.FIELD

@Statistic(name = "Test 1")
public String test;

ElementType.METHOD

@Statistic(name = "Test 2")
public List<Stuff> findAll() {
    return findAll("order by added desc");
}
jasony
  • 1
  • 1