34

AClass.class.getName();

if AClass is a java class, this method will return package name and class name. but when i convert AClass java file to Kotlin file ,it will only return a class name. so system cannot find this class path

the code above

4
  • I'm not a professional in Kotlin, but you could try AClass.class.getPackage().getName(); and append that to your AClass.class.getName(); Like so: String location = AClass.class.getPackage().getName() + "." + AClass.class.getName(); Commented Jun 14, 2017 at 4:01
  • thanks, Class.forName(fragmentName) if fragmentName is a Kotlin class ,it will throws ClassNotFoundException. not matter fragmentName is complete name. so i was confused Commented Jun 14, 2017 at 6:34
  • Did that fix it? Commented Jun 14, 2017 at 14:21
  • val c = Class.forName(fragmentName)?.newInstance() fragment=c as Fragment fragmentName is a KClass complete name, it solves my problem Commented Jun 15, 2017 at 8:12

5 Answers 5

55

there are many ways to get the full qualified name of a java Class in kotlin:

get name via the property KClass.qualifiedName:

val name = AClass::class.qualifiedName;

OR get name via the property Class.name:

val name = AClass::class.java.name;

OR get name via the method Class#getName:

val name = AClass::class.java.getName();

the table of the qualified name of a class as below:

|-----------------------|-----------------------|-----------------------|
|                       |          Class        |     Anonymous Class   |
|-----------------------|-----------------------|-----------------------|
| KClass.qualifiedName  |    foo.bar.AClass     |         null          |
|-----------------------|-----------------------|-----------------------|
| Class.name            |    foo.bar.AClass     |    foo.bar.AClass$1   |
|-----------------------|-----------------------|-----------------------|
| Class.getName()       |    foo.bar.AClass     |    foo.bar.AClass$1   |
|-----------------------|-----------------------|-----------------------|
Sign up to request clarification or add additional context in comments.

4 Comments

If you added an example input sample and output of each described option, that'd be fantastic.
@EugenPechanec thanks for your feedback, sir. how about it now?
thanks, actually this is want i want
I believe the second and third techniques are exactly the same, as Kotlin exposes Java methods beginning with .get as properties: kotlinlang.org/docs/java-interop.html#getters-and-setters
10

If it is a java fragment

var fragmentSimpleName = FragmentName::class.java.simpleName as String

1 Comment

Thanks. As I understood, the same is for Kotlin fragment.
9

Try below solution::-

var name = MainActivity::class.java.canonicalName as String

2 Comments

Actually,It just a few code be covered into Kotlin, AClass.class.getName();this code above was invoked in java class , so i cannot use Kotlin syntax like your answer.
Actually,It just a few code be covered into Kotlin, AClass.class.getName();this code above was invoked in java class , so i cannot use Kotlin syntax like your answer. i want a complete file name,because Class.forName(fragmentName) will be invoked in next. fragmentName should be replace by AClass complete name. but AClass is a Kotlin class now, it throws ClassNotFoundException
6

This is what I use to get class-name.

    val TAG = javaClass.simpleName

For Android developer's, it's very useful to declare as a field, and call to print logs.

4 Comments

And what is javaClass?
javaClass is the class in which you type. just type javaClass in any class it'll work.
Thanks! It shouldn't be used as a constant in companion object, rather like a variable. In compaion object we can use val TAG = YourClass::class.java.simpleName.
@CoolMind That is long solution, you've to type your class name everywhere you use it. Whereas my line of code can be copied to any class and can be used without any modification.
1

Maybe I am a little bit late for the party, but I do it using hash code of the new instance of the fragment. It is an Int so allows all kinds of tests.

 private val areaFragment by lazy { Area_Fragment.newInstance() }

    var fragmentHashCode = fragment.hashCode()
            when (fragmentHashCode) {
                areaFragment.hashCode() -> {
                    myNavigationView.setCheckedItem(R.id.nav_area)
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.