function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShravanKumarBagamShravanKumarBagam 

What is a Instanceof keyword

Hi,

  

Can anybody explain what is instanceof keyword in salesforce with small example?

 

In which scenario we can use this..

 

bob_buzzardbob_buzzard

The instanceof keyword allows you to check the if an object is an instance of a class.  Thus you could have something which is passed in as simply an Object type, and you can then determine if it is an instance of your custom class and take appropriate action, e.g.

 

public void myMethod(Object myObj)
{
   if (myObj instanceof MyClass)
   {
      // cast the object to the appropriate type and do something
      MyClass myClassInst=(MyClass) myObj;
   }
}

 

Subrat kumar RaySubrat kumar Ray
MyCustomClass varClass = new MyCustomClass();
Boolean checkClass = varClass instanceof MyCustomClass;

Here 'MyCustomClass' is the name of your apex class.
Returns boolean result.