Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you use Java annotations? [duplicate]

Possible Duplicates:
How and where are Annotations used in Java?
Java beans, annotations: What do they do? How do they help me?

Over and over, I read about Java 5's annotations being an 'advanced feature' of the language. Until recently, I haven't much used annotations (other than the usual @Override, &c), but work on a number of webservice-related projects has forced my hand. Since I learned Java pre-5, I never really took the time to sit down and grok the annotation system.

My question- do you guys actually use annotations? How helpful are they to you, day-to-day? How many StackOverflow-ers have had to write a custom annotation?

like image 712
Matt Luongo Avatar asked Mar 01 '10 18:03

Matt Luongo


People also ask

Can we apply two annotations together?

It is also possible to use multiple annotations on the same declaration: @Author(name = "Jane Doe") @EBook class MyClass { ... } If the annotations have the same type, then this is called a repeating annotation: @Author(name = "Jane Doe") @Author(name = "John Smith") class MyClass { ... }

Can you apply multiple annotations in target Yes or no?

It is now possible to use an annotation zero times, once, or, if the annotation's type is marked as @Repeatable , more than once. It is also possible to restrict where an annotation type can be used by using the @Target meta-annotation.

How do you make annotations repeatable?

Repeatable Annotation: The Repeating Annotation has to be annotated with @Repeatable to have the capability to repeat. We have to pass the class name as a parameter to the meta-annotation @Repeatable where the compiler can store all the multiple annotations and that acts as a container for values.

How do you use annotations in Java?

Annotations are used to provide supplemental information about a program. Annotations start with '@'. Annotations do not change the action of a compiled program. Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.


1 Answers

Perhaps the most useful and used case of Java Annotations is to use POJO + Annotation instead of xml configuration files

I use it a lot since (as you already stated) if you use a web framework (like spring or seam) they usually have plenty of annotations to help you.

I have recently wrote some annotations to build a custom statemachine, validations purpose and annotations of annotations (using the metadata aspect of it). And IMO they help a lot making the code cleaner, easier to understand and manage.

like image 182
Diego Dias Avatar answered Oct 18 '22 14:10

Diego Dias