78

I am aware that using Context and a method getClass().getName() I can get a string which represent full class name, like com.package1.package2.MainActivity.

How can I get only the last part, class name only? In this case it would be MainActivity string.

I can do it with a simple split() method, but maybe there is a better way, more reliable.

10 Answers 10

179

This is all you need.

MainActivity.this.getClass().getSimpleName();
Sign up to request clarification or add additional context in comments.

1 Comment

This is the right thing. The getLocalClassName() will return any package name within the root of the app. This returns class' name only, no matter if it's in root or in some other package.
22

To get only the name of the class, not full path you will use this expression:

String className =  this.getLocalClassName(); 
//or
String className = getBaseContext().getLocalClassName();  
//or
String className = getApplicationContext().getLocalClassName(); 

3 Comments

Every day is a school day, nice find :-)
Yes, I hate for being so soon after I posted a question, but it's better I posto it this way than to delete a question.
do not rely on both 3 if you on old API, null or useless text will return. For me only getClass().getName() works
8

Alternatively, you could use:

private static final String TAG = MainActivity.class.getSimpleName();

Comments

3
  1. If you need class name within a method, use getLocalClassName()

  2. If you need class name outside a method, use getClass().getSimpleName()

  3. If you want to reuse the class name in multiple methods within the same class, then use private final String TAG = getClass().getSimpleName(); within the class and then use TAG variable in every method.

  4. If you want to access the class name from static methods, then use private static final String TAG = MainActivity.class.getSimpleName(); now use the static variable TAG within your static methods.

Comments

1

No matter what way you do it, you'll need to perform an extra operation on top of getClass. I'd recommend this over split:

String className = xxx.getClass();
int pos = className.lastIndexOf ('.') + 1; 
String onlyClass = className.substring(pos);

3 Comments

read last line of his post he wants other then split(substring)
That's fine, I didn't use split.
@Rawkode There is a better way. This one is more secure than using split or substring.
1

Use the getSimpleName method:

String name = getClass().getSimpleName();

Comments

1

In kotlin you should use:

val className = javaClass.simpleName

Comments

1

Kotlin way: MainActivity::class.java.simpleName

Comments

0

No other solutions work for me, dunno why. I'm using

this.getClass().getName().replace("$",".").split("\\.")[3]

cons: in one string, and you can use it in threads and listeners.

(in listeres I get something like com.djdance.android.main$53)

Comments

0

If you want to get the name outside the class you can call:

MainActivity.class.getSimpleName()

in this way you can avoid to make your class static.

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.