Questions tagged [annotations]

In programming, annotations are used to add information to a code element which cannot be expressed by the type system.

In programming, annotations are used to add information to a code element which cannot be expressed by the type system.

Java annotations

Up to annotations were only usable inside comments and were used for denoting special information like the author of a class or method or references to other entities.

In some cases they were also used for code generation, by facilitating the toolchain.

As these annotations were part of the code, it was not possible to use them in any way at run time since they were not part of the byte code.

With , annotations became a proper part of java syntax. Annotations can be defined using a syntax similar to the definition of interfaces. They can be used to annotate classes, methods, fields, parameters and packages.

Depending on the definition an annotation is available in source code, byte code or run time. Therefore they can be used for code generation, byte code manipulation at class loading time and via reflection at run time.

For info more see Wiki page and

C# Attributes

Attributes are a similar concept to Java annotations, they provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.

13026 questions
29 answers

What's the difference between @Component, @Repository & @Service annotations in Spring?

Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device? In other words, if I have a Service class and I change the annotation from…
Colin McCree
  • 25,405
  • 3
  • 15
  • 3
25 answers

Which @NotNull Java annotation should I use?

I'm looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions. Many of the tools seem incompatible with each others'…
  • 13,471
  • 4
  • 19
  • 19
11 answers

What does -> mean in Python function definitions?

I've recently noticed something interesting when looking at Python 3.3 grammar specification: funcdef: 'def' NAME parameters ['->' test] ':' suite The optional 'arrow' block was absent in Python 2 and I couldn't find any information regarding its…
  • 9,561
  • 3
  • 15
  • 12
15 answers

Difference between and

I'm learning Spring 3 and I don't seem to grasp the functionality behind and . From what I've read they seem to handle different annotations (@Required, @Autowired etc vs @Component, @Repository,…
  • 7,211
  • 3
  • 14
  • 3
16 answers

Spring: @Component versus @Bean

I understand that @Component annotation was introduced in spring 2.5 in order to get rid of xml bean definition by using classpath scanning. @Bean was introduced in spring 3.0 and can be used with @Configuration in order to fully get rid of xml file…
  • 7,101
  • 3
  • 14
  • 4
19 answers

Where does the @Transactional annotation belong?

Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes which are calling using the DAO objects? Or does it make sense to annotate both "layers"?
Thomas Einwaller
  • 8,873
  • 4
  • 40
  • 55
7 answers

Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

What is the main difference between @Before and @BeforeClass and in JUnit 5 @BeforeEach and @BeforeAll @After and @AfterClass According to the JUnit Api @Before is used in the following case: When writing tests, it is common to find that…
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
27 answers

When do you use Java's @Override annotation and why?

What are the best practices for using Java's @Override annotation and why? It seems like it would be overkill to mark every single overridden method with the @Override annotation. Are there certain programming situations that call for using the…
Alex B
  • 24,678
  • 14
  • 64
  • 87
15 answers

Should we @Override an interface's method implementation?

Should a method that implements an interface method be annotated with @Override? The javadoc of the Override annotation says: Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is…
Benno Richters
  • 15,378
  • 14
  • 42
  • 45
14 answers

JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

I am getting the following error when trying to get a JSON request and process it: No suitable constructor found for type [simple type, class com.myweb.ApplesDO]: can not instantiate from JSON object…
Lucky Murari
  • 12,672
  • 5
  • 22
  • 43
11 answers

@Resource vs @Autowired

Which annotation, @Resource (jsr250) or @Autowired (Spring-specific) should I use in DI? I have successfully used both in the past, @Resource(name="blah") and @Autowired @Qualifier("blah") My instinct is to stick with the @Resource tag since it's…
  • 6,663
  • 6
  • 33
  • 26
10 answers

Only using @JsonIgnore during serialization, but not deserialization

I have a user object that is sent to and from the server. When I send out the user object, I don't want to send the hashed password to the client. So, I added @JsonIgnore on the password property, but this also blocks it from being deserialized into…
  • 37,646
  • 24
  • 106
  • 138
4 answers

@Nullable annotation usage

I saw some method in java declared as: void foo(@Nullable Object obj) { ... } What's the meaning of @Nullable here? Does it mean the input could be null? Without the annotation, the input can still be null, so I guess that's not just it?
  • 9,223
  • 14
  • 45
  • 57
6 answers

What's the difference between interface and @interface in java?

I haven't touched Java since using JBuilder in the late 90's while at University, so I'm a little out of touch - at any rate I've been working on a small Java project this week, and using Intellij IDEA as my IDE, for a change of pace from my regular…
  • 11,753
  • 10
  • 58
  • 76
13 answers

How do I assert my exception message with JUnit Test annotation?

I have written a few JUnit tests with @Test annotation. If my test method throws a checked exception and if I want to assert the message along with the exception, is there a way to do so with JUnit @Test annotation? AFAIK, JUnit 4.7 doesn't provide…
  • 5,612
  • 10
  • 33
  • 37
2 3
99 100