Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

I would like to know the class of a variable/property at runtime. For example:

Integer i = 5;


if (i.className == 'Integer') {

    System.debug('This is an integer.');

} else {

    System.debug('This is not an integer, but a ' + i.className);


I can't find the method/property that returns the class type in the documentation (assuming it's there). Am I missing it?

1 Answer

0 votes
by (32.1k points)
If you want to verify at runtime whether an object is an instance of a particular class, you can use the instanceof keyword to do so.

But, you can't use the instanceof keyword on an instance of the class of its subclasses or else, you'll receive a compile error. You can only use the instanceof keyword on superclasses.

Browse Categories