dor506
dor506

Reputation: 5414

Kotlin - equivalence to SomeClass.class for objects?

I'm trying to statically get an object's name (For logging uses)

The equivalence for:

public class SomeClass
{
    private static final String TAG = SomeClass.class.getSimpleName()
}

In Kotlin:

object SomeObject
{
   private const val TAG = ?
}

Upvotes: 3

Views: 504

Answers (2)

Sachin Chandil
Sachin Chandil

Reputation: 17829

If you are using it in Android application, i would suggest you to do it like following: (packageName:className)

object SomeObject
{
    private val TAG = "${SomeObject::class.java.`package`.name}:${SomeObject::class.simpleName}"
}

Doing so you can ensure that tag name for any other class wont be duplicate. It is really helpful, if you are creating a lib/code snippet that someone can use. Otherwise having same tag name may lead to runtime errors.

Upvotes: 0

Neo
Neo

Reputation: 1201

Try this

SomeClass::class.simpleName

Upvotes: 5

Related Questions