29
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kotlinでクラスオブジェクトを参照する(Javaのクラス名.classの代わり)

Last updated at Posted at 2017-06-05

こんにちはsekitakaです。

javaでクラス名.classをKotlinではどのように記述すればよいのか迷ったのでメモしておきます。

staticに取得

以下のように::class.javaを使うことで、クラスオブジェクトを使う事ができます。

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

インスタンスから取得

@meeniex さん頂いたコメントを元に追記。
インスタンスからは以下のように参照できます。

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?