Open In App

How to Iterate Over a Class’s Properties in Kotlin?

Improve
Improve
Like Article
Like
Save
Share
Report

Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a “better language” than Java, but still be fully interoperable with Java code. Reflections in Kotlin allow us to introspect on the structure of our program at runtime. This also enables us to introspect the class modifiers, methods, and properties. In this article, we will see how we can iterate over the properties of a Kotlin class. So let’s get started!

Example

In the following steps, we will see how to iterate over a class’s properties:

1. Here’s our Student class with the roll_number and full_name attributes:

class Student constructor (var roll_number : Int, var full_name : String)

2. Now, we will be using a for statement, because we want to iterate over multiple properties that a class can have:

Kotlin




fun main (args: Array<String>) {
  var student=Student (2013001, "Alok Yadav" )
  for (property in Student : : class. memberProperties){
    println ("${property . name) = ${property . get (student) }")
  }
}


Output:

full_name = Alok Yadav
roll_number = 2013001

The implementation is quite straightforward. We are able to achieve introspection into the class’s properties because we are using reflections and memberProperties is just one of the many functions of KClass. One thing to note is that memberProperties returns all the non-extension properties declared in this class and all of its superclasses. Consider that we have a Person class, as follows:

Kotlin




open class Person{
  val isHuman : Boolean = true
}


Also, we extend our Student class with the Person class, and then the same code used earlier with the memberProperties method will result in output as shown:

full name = Aanand Shekhar Roy
roll_number = 2013001
isHuman = true

So, if you want to just iterate over the declared fields in the Student class, you will need the declaredMemberProperties method. Here’s an example with declaredMemberProperties:

Kotlin




for (property in Student :: class.declaredMemberProperties) {
  println ("${property.name} = ${property.get(student)}")
}


Output:

full_name = alok yadav
roll_number = 2013001

The preceding examples were for Kotlin kClass. Suppose you want to iterate over properties for a Java Class<T> you can use a Kotlin extension property to get the Kotlin KClass<T>, from which you can proceed, for example,

something.javaClass.kotlin.memberProperties.

There is a list of methods provided by Kotlin’s Reflection library, with the help of which you can perform a lot of introspection at runtime. For that check it at kotlinlanf.org or we can say Kotlin official documentation and you can click here.



Last Updated : 22 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads