Javatpoint Logo
Javatpoint Logo

Kotlin Reflection

Reflection is a set of language and library features that examines the structure of program at runtime. Kotlin makes functions and properties as first-class citizen in the language and examine these functions and properties at runtime.

Class Reference

Class reference is used to obtain the reference of KClass object. To obtain the reference of statically Kclass, we should use the class literal(i.e. use double colons).

Syntax of class reference:

The reference value is a class type of KClass. KClass class reference is not the same as a Java class reference. We obtain the Java class reference by using .java property on a KClass instance.

Note: KClass represents a class and provides examination capabilities. To obtain the instance of this class use syntax ::class.

Functional Reference

Kotlin functional is used to obtain the reference of function using double colons. The reference of function can be used in another function as a parameter. To use this reference in another function we use the :: operator:

Kotlin functional reference example



In the above program ::isPositive is a value of function type (Int) -> Boolean.

Overloaded function reference operator (::)

The operator :: can be used with overload function when the expected type is known from the context. For example:

Create a function isPositive() which takes two different types Int and String and call this function with different type parameter.


[5, 10]

Property Reference

We can also access the properties as first-class object in Kotlin, to access object property we can use :: operator:

To evaluate the property object of type KProperty<Int> we use the expression ::variableName. The expression ::variableName allow to retrieve its property name by using name and readits value using get() function.

To reset the value of mutable type property, reference property has set() method.



Access the property of member class:

Property reference also access the property of other member of class. For example:



Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA