# Confusion about @field:Annotation

**URL:** <https://discuss.kotlinlang.org/t/confusion-about-field-annotation/26272>\
**Category:** Support\
**Created:** [January 25, 2023, 9:53am UTC](https://discuss.kotlinlang.org/t/confusion-about-field-annotation/26272 "2023-01-25T09:53:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SvenWollinger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svenwollinger/32/10122_2.png) [@SvenWollinger](https://discuss.kotlinlang.org/u/SvenWollinger)\
**Post date:** [January 25, 2023, 9:53am UTC](https://discuss.kotlinlang.org/t/confusion-about-field-annotation/26272/1 "2023-01-25T09:53:28Z")

</div>

Hi! Im trying to use JVM Reflection to get the Annotations.  
By doing this:

```auto
clazz.javaClass.declaredFields.forEach {
    it.trySetAccessible()
    val annotations = it.getAnnotationsByType(MyAnnotation::class.java)
    ...
}

```

This always comes back as empty, unless i use it with @field:MyAnnotation, why do i need the field:?

---

<div class="post-metadata">

**Author:** ![LSafer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lsafer/32/9366_2.png) [@LSafer](https://discuss.kotlinlang.org/u/LSafer)\
**Post date:** [January 25, 2023, 8:09pm UTC](https://discuss.kotlinlang.org/t/confusion-about-field-annotation/26272/2 "2023-01-25T20:09:31Z")

</div>

You are relying on JVM reflection and it is different that kotlin’s reflection

Kotlin does not translate 1-1 to Java.

Fields in kotlin are called properties and are translated into  
multiple components, a getter, setter and a backing field (depending on the declaration)

This way, a property can be overriden, delegated and/or have a custom setter/getter

When a property is annotated, since java doesn’t have such element (kotlin properties), ~~the annotation isn’t translated to java.~~  
~~And kotlin doesn’t know what component to annotate. The getter? the setter? or the backing field?~~  
It is needed to guide kotlin on what component should be annotated. Or rely on the default kotlin behavior.

And that is why sometimes it is needed to tell kotlin to explicitly annotate the backing field.

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [January 25, 2023, 10:06pm UTC](https://discuss.kotlinlang.org/t/confusion-about-field-annotation/26272/3 "2023-01-25T22:06:25Z")

</div>

> [@LSafer](#):
>
> And kotlin doesn’t know what component to annotate. The getter? the setter? or the backing field?

This is incorrect, Kotlin knows what to annotate, the rules are here:

> **[Annotations | Kotlin](https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets)**

If both `property` and `field` are valid targets for the annotation, the `property` is annotated, Java will hence see an annotation on the getter.

---

<div class="post-metadata">

**Author:** ![LSafer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lsafer/32/9366_2.png) [@LSafer](https://discuss.kotlinlang.org/u/LSafer)\
**Post date:** [January 25, 2023, 10:44pm UTC](https://discuss.kotlinlang.org/t/confusion-about-field-annotation/26272/4 "2023-01-25T22:44:55Z")

</div>

I meant that in a metaphorical way.

Still, the annotation isn’t put on all the generated components (Specially the generated backing field) without a `@field:` specifier

---

<div class="post-metadata">

**Author:** ![LSafer](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lsafer/32/9366_2.png) [@LSafer](https://discuss.kotlinlang.org/u/LSafer)\
**Post date:** [January 26, 2023, 10:55am UTC](https://discuss.kotlinlang.org/t/confusion-about-field-annotation/26272/5 "2023-01-26T10:55:01Z")

</div>

@al3c Actually, you are right 😗

> [@al3c](#):
>
> If both `property` and `field` are valid targets for the annotation, the `property` is annotated, Java will hence see an annotation on the getter.

The following will output `[true]`

```kotlin
@Target(AnnotationTarget.FIELD)
@Retention(AnnotationRetention.RUNTIME)
annotation class ExampleAnnotation

class ExampleClass {
    @ExampleAnnotation
    val exampleProperty: String = ""
}

fun main() {
    val clazz = ExampleClass::class.java
    clazz.declaredFields.forEach { field ->
        println(field.annotations.map { it is ExampleAnnotation })
    }
}

```

I didn’t knew about this exact behavior.

In my defense, the question was about why it is needed to add `@field:` and not is it always necessary.

But, I am so sorry for the false statement in my first answer
