I'm looking for a way to get the name of an object instance at run-time. Is it possible to to determine the class name (as a string) of an instance? i.e. is there an Apex equivalent of the Java myInstance.getClass().getName() or any tricks to achieve just that?

  • String.valueOf(myInstance) will get you pretty close - works with built in classes too... class MyClass {} System.debug(String.valueOf(new MyClass())); //MyClass:[] System.debug(String.valueOf(new PageReference(''))); //System.PageReference[] Commented Jan 16, 2013 at 16:36
  • 1
    Yes - thanks, only trouble is with that if someone has overridden the toString() method in the class it may return something other than the class name, i.e. you can't always rely on it. Commented Jan 16, 2013 at 16:40
  • I thought you owned all the classes :P Commented Jan 16, 2013 at 16:56
  • 1
    @user320 - lol, yes I do and I know that I have overridden toString() (for a good reason too) and that it won't work in my use case. Commented Jan 16, 2013 at 17:05
  • @user320 your idea works well unless myInstance has a value of null. How would one work around this?
    – cropredy
    Commented Dec 26, 2013 at 22:15

4 Answers 4


instanceOf verifies at runtime whether an object is actually an instance of a class...but you would have to write the conditions to check for them all. Also getSObjectType will describe an sObject.

  • 1
    Thanks, actually I've just realised that since I own all of the classes I'm interested in I can implement my own getName() method. Commented Oct 16, 2012 at 19:24

Voting for the idea Method to get the Type of an Object (also Primitive Type not only SObject) would be a good long term solution.


From Trailhead's Apex Recipes, covers Inner Classes as well.

     * @description It can be useful to know what the type of an object
     * is at runtime. This is especially useful when you're dynamically
     * instantiating objects in code from the name of a class.
     * I'm not generally a fan of relying on an exception to make logic
     * decisions, but in this case I'll make an exception as this is the
     * cleanest method I've yet found for efficently determining an objects
     * class name that covers edge cases where the object is of an inner class'
     * type.
     * This method parses a TypeException for the true name of a class.
     * It generates the exception by forcing a cast from the Object parameter
     * to DateTime. If no typeException occurs, we know it's a dateTime object.
     * @param obj
     * @return      `String`
    public static String getUnkownObjectType(Object obj) {
        String result = 'DateTime';
        try {
            DateTime typeCheck = (DateTime) obj;
        } catch (System.TypeException expectedTypeException) {
            String message = expectedTypeException.getMessage()
                .substringAfter('Invalid conversion from runtime type ');
            result = message.substringBefore(' to Datetime');

        return result;

You can use something like below:


@pbattisson's Ans
@Matthew's Ans in SF blog

  • 2
    this doesn't return the fully qualified name for inner classes.
    – NSjonas
    Commented Oct 10, 2016 at 18:17
  • Agreed, I didn't try it for inner class though. Do we have any work around to get the name of inner class? Thanks Commented Oct 13, 2016 at 17:39
  • Caveat: The bigger the class, the more CPU is needed for String.valueOf(this) Commented Aug 30, 2022 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .