Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a quick approach to something like:

  MyClass mc = new MyClass();
  String theName = ClassNameProvider.getName(mc); // returns 'MyClass'
share|improve this question
I've upvoted @pbattisson's answer but for some crazy solution (if you'd need the method name too) go check out… ;) – eyescream Jan 11 '14 at 15:18
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. – Daniel Ballinger Jun 3 '15 at 2:38
up vote 25 down vote accepted

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.

share|improve this answer
harder, better, faster, stronger – bigassforce Jan 11 '14 at 13:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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