35

I need a quick approach to something like:

  MyClass mc = new MyClass();
  String theName = ClassNameProvider.getName(mc); // returns 'MyClass'
2
52

I think your requirement falls in the gap for apex but we have the ability to do one of following things:

We can retrieve the name of an object using code such as

String name = MyClass.class.getName(); //returns MyClass

We can check whether the instance of a particular type using

Boolean isSame = mc instanceof MyClass; //returns true

Or if we want to get the name from an instance of an unknown type we can do:

String name = String.valueOf(mc).split(':')[0];//returns MyClass

Hopefully one of those covers your need. See the discussion to a similar question here for more info.

4
  • 6
    harder, better, faster, stronger Jan 11 '14 at 13:38
  • 4
    NOTE: String.valueOf(mc).split(':')[0] will not include the top level class for inner classes
    – NSjonas
    Oct 10 '16 at 18:15
  • 1
    NOTE: That String.valueOf(mc) returns the toString() value of the class. The default return value is format of ClassName:[field1=value1, field2=value2,...fieldN=valueN] but if the class overrides the toString() method then this trick may not work for you.
    – Doug Ayers
    Jun 22 '18 at 4:54
  • Worth considering: String name = String.valueOf( mc ).substringBefore( ':' ) as it's just a little bit clearer Nov 7 '19 at 11:42
10

This is an interesting hack as posted on the idea Method to get the Type of an Object (also Primitive Type not only SObject) by Robert Strunk:

public static String getObjectType(Object obj){

    String result = 'DateTime';
    try{
        DateTime typeCheck = (DateTime)obj;
    }
    catch(System.TypeException te){

        String message = te.getMessage().substringAfter('Invalid conversion from runtime type ');
        result = message.substringBefore(' to Datetime');
    }

    return result;
}

The parse conversion exception approach overcomes the caveat that using String.valueOf(obj).split(':')[0] does not work in scenarios where the Apex class' toString() method has been overriden and returns a value in a different format, as pointed out in this comment.

4

To answer the title of the question: to get the name of the class from within any class use this:

String CurrentClassName = String.valueOf(this).substring(0,String.valueOf(this).indexOf(':'));
3
  • 2
    Note the accepted answer recommends String.valueOf(mc).split(':')[0] among other approaches. This approach is just a slower version of that (you take String.valueOf twice).
    – Adrian Larson
    Apr 7 '17 at 13:17
  • 1
    Actually, sometimes Paul's answer works better. Split-approach in some cases, for example within a loop, could reach a "Regex is too complicated" Salesforce Limit.
    – kvor
    Jul 5 '19 at 9:04
  • 1
    This could be improved by saving String.valueOf(this) in a string, which is then referenced twice. But, it beats all the other methods becuase they all need a line with the Class name in it in the first place - just save it as a String if you are going to do that. This method works, exactly as it is, everywhere!
    – cyberspy
    May 26 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.