01 logo

Explore Kotlin Annotations

Kotlin annotations which help in programming with Java

By Shalu TDPublished 3 years ago 3 min read
2
Explore Kotlin Annotations
Photo by Kaitlyn Baker on Unsplash

This article discusses the Kotlin annotations used while developing the Android app. Here we will explore the annotations @JvmStatic, @JvmOverloads, and @JvmField.

As Google suggests Kotlin as the official language for Android development, most of the people are in the process of migrating existing Java code to Kotlin code. But we can’t convert all source code from Java to Kotlin in a single sprint itself

So, we want to convert some part of the code (Classes, enums, etc) to Kotlin and mix with the existing java code. If you are in this process, you should know these annotations.

@JvmStatic

In Kotlin, the package-level functions are represented as static methods. Also, in Kotlin, you can make static methods for functions that are defined in some companion object or named object by using the @JvmStatic annotation.

@Target([AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER])

annotation class JvmStatic

It specifies that an additional static method needs to be generated from this element if it’s a function. If this element is a property, additional static getter/setter methods should be generated.

Consider the below class:

class ClassName {

companion object {

@JvmStatic

fun iAmStatic() {

//body of Static function

}

fun iAmNonStatic() {

//body of Non static function

}

}

}

Here, iAmStatic() is a static function and iAmNonStatic() is a non-static function. Suppose you are calling the above methods from the Java class. It will behave like below:

ClassName.iAmStatic(); // works fine

ClassName.iAmNonStatic(); // compile error

ClassName.Companion.iAmStaticMethod(); // works fine

ClassName.Companion.iAmNonStaticMethod(); // other way to work

As for the case of named objects, the same procedure is applicable:

object ObjectName {

@JvmStatic fun iAmStatic() {

//body of Static function

}

fun iAmNonStatic() {

//body of Non static function

}

}

Call the above methods from Java as below:

ObjectName.iAmStatic(); // works fine

ObjectName.iAmNonStatic(); // compile error

ObjectName.INSTANCE.iAmStatic(); // works

ObjectName.INSTANCE.iAmNonStatic(); // works

@JvmOverloads

@Target([AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR]) annotation class JvmOverloads

It instructs the Kotlin compiler to generate overloads for this function that substitute default parameter values. If a method has N parameters and M of which have default values, M overloads are generated: the first one takes N-1 parameters (all but the last one that takes a default value), the second takes N-2 parameters, and so on.

Consider a class having two fields, one is initialised and the other is not initialised.

data class Customer( val name: String, val dob: Date = Date())

Here in this class, by default, the current date will be taken as the date of birth of the customer if we are not passing the second argument while creating the customer object. So, if we are calling from Kotlin, the following code will run without any compile error.

val custOne = Customer("Don")

val custTwo = Customer("Don", Date())

But if we are calling the same from the Java then you have to pass all the parameters otherwise you will get a compile error:

Customer custOne = new Customer("Don"); //Here we are passing only one argument, so we will get compile error

Customer custTwo = new Customer("Don", new Date());//No error

In order to make use of the default value while creating an object from Java, we can use the annotation @JvmOverloads. Now, after using the annotation, the Kotlin code will be:

Customer @JvmOverloads constructor( val name: String, val dob: Date = Date())

Now, calling from Java, you don’t need to pass all the parameters:

Customer custOne = new Customer("Don"); //No Error

Customer custTwo = new Customer("Don", new Date());//No error

@JvmField

@Target([AnnotationTarget.FIELD]) annotation class JvmField

It instructs the Kotlin compiler not to generate getters/setters for this property and expose it as a field.

Consider the below Java class:

public class Customer {

public String name;

public Date dob;

public getName() {

return this.name;

}

public getDob() {

return this.dob;

}

}

The corresponding Kotlin code will be as below :

data class Customer( val name: String, val dob: Date = Date())

If you are accessing the properties of the class, then the code in Kotlin is:

val customer = Customer("Don", Date())

val name = customer.name

val dob = customer.dob

But in Java, we have to use the getter method as below:

Customer customer = new Customer("Don", new Date());

String name = customer.getName();

Date dob = customer.getDob();

If you want a particular field to be used as a normal field and not as a getter or setter then you have to tell the compiler not to generate any getter and setter for the same and this can be achieved by using the @JvmField annotation. So, the Kotlin code after using the @JvmField annotation will be:

data class Customer(@JvmField val name: String, val dob: Date = Date())

Now, you can access the field from Java as below:

Customer customer = new Customer("Don", new Date());

String name = customer.name;

And you are ready to go. Thanks for reading!

how to
2

About the Creator

Shalu TD

Android Engineer | Blogger | Mentor

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.