Browse class objects in Kotlin (instead of Java class name.class)

Hello sekitaka.

I was wondering how to write class name.class in java in Kotlin, so make a note of it.

Get static

You can use a class object by using :: class.java as shown below.

val jclass = String::class.java
val kclass = String::class.java.kotlin
println(jclass) // class java.lang.String
println(kclass) // class kotlin.String

Get from instance

Added based on the comment received by @meeniex. You can refer to it from the instance as follows.

val jclass = s.javaClass
val kclass = s.javaClass.kotlin 
println(jclass) // class java.lang.String
println(kclass) // class kotlin.String

Recommended Posts

Browse class objects in Kotlin (instead of Java class name.class)
A quick review of Java learned in class
Why use setters/getters instead of public/private in Java
A quick review of Java learned in class part4
Write a class in Kotlin and call it in Java
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Use of Abstract Class and Interface properly in Java
Summarize the additional elements of the Optional class in Java 9
[Java] Comparator of Collection class
[Java] Get KClass in Java [Kotlin]
Summary of Java Math class
Implementation of gzip in java
Implementation of tri-tree in Java
Implementation of HashMap in kotlin
Various methods of Java String class
Kotlin Class to send to Java developers
Output the difference between each field of two objects in Java
Handle business logic for a set of Entity in Java class
Sort a List of Java objects
StringBuffer and StringBuilder Class in Java
Mechanism and characteristics of Collection implementation class often used in Java
Deleting AWS S3 Objects in Java
List of members added in Java 9
Examine the list of timezone IDs available in the Java ZoneId class
Concatenate strings returned by methods of multiple objects in Java Stream
Generics of Kotlin for Java developers
List of types added in Java 9
Implementation of like function in Java
[Java] I participated in ABC-188 of Atcorder.
What is a class in Java language (3 /?)
Implementation of DBlayer in Java (RDB, MySQL)
Get the result of POST in Java
Kotlin Class part.2 to send to Java developers
[Java] Inheritance and structure of HttpServlet class
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Easily measure the size of Java Objects
Creating a matrix class in Java Part 1
What Java programmers find useful in Kotlin
The story of writing Java in Emacs
Role of JSP in Web application [Java]
Discrimination of Enums in Java 7 and above
[Java] Why use StringUtils.isEmpty () instead of String.isEmpty ()
Summarize the life cycle of Java objects to be aware of in Android development
[Java] Where is the implementation class of annotation that exists in Bean Validation?
Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)
Cause of is not visible when calling a method of another class in java
GetInstance () from a @Singleton class in Groovy from Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
About the idea of anonymous classes in Java
Java method call from RPG (method call in own class)
The story of learning Java in the first programming
[Java] use Optional instead of if (hoge! = null) {...}
Measure the size of a folder in Java
Recommendation of Service class in Ruby on Rails
[Java] Use of final in local variable declaration
Feel the passage of time even in Java
How to get Class from Element in Java
Basics of threads and Callable in Java [Beginner]