Use the forName methods to retrieve the type of an Apex class, which can be a built-in or a user-defined class. Also, use the newInstance method if you want to instantiate a Type that implements an interface and call its methods while letting someone else, such as a subscriber of your package, provide the methods’ implementations.
The following sample shows how to use the Type methods to instantiate a Type based on its name. A typical application of this scenario is when a package subscriber provides a custom implementation of an interface that is part of an installed package. The package can get the name of the class that implements the interface through a custom setting in the subscriber’s org. The package can then instantiate the type that corresponds to this class name and invoke the methods that the subscriber implemented.
In this sample, Vehicle represents the interface that the VehicleImpl class implements. The last class contains the code sample that invokes the methods implemented in VehicleImpl.
global class VehicleImpl implements Vehicle { global Long getMaxSpeed() { return 100; } global String getType() { return 'Sedan'; } }
public class CustomerImplInvocationClass { public static void invokeCustomImpl() { // Get the class name from a custom setting. // This class implements the Vehicle interface. CustomImplementation__c cs = CustomImplementation__c.getInstance('Vehicle'); // Get the Type corresponding to the class name Type t = Type.forName(cs.className__c); // Instantiate the type. // The type of the instantiated object // is the interface. Vehicle v = (Vehicle)t.newInstance(); // Call the methods that have a custom implementation System.debug('Max speed: ' + v.getMaxSpeed()); System.debug('Vehicle type: ' + v.getType()); } }
The class property returns the System.Type of the type it is called on. It is exposed on all Apex built-in types including primitive data types and collections, sObject types, and user-defined classes. This property can be used instead of forName methods.
System.Type t = Integer.class;
You can use this property for the second argument of JSON.deserialize, deserializeStrict, JSONParser.readValueAs, and readValueAsStrict methods to get the type of the object to deserialize. For example:
Decimal n = (Decimal)JSON.deserialize('100.1', Decimal.class);
The following are methods for Type.
public Boolean equals(Object typeToCompare)
Type: Boolean
Type t1 = Account.class; Type t2 = Type.forName('Account'); System.assert(t1.equals(t2));
public static System.Type forName(String fullyQualifiedName)
Type: System.Type
public static System.Type forName(String namespace, String name)
Type: System.Type
This example shows how to get the type that corresponds to the ClassName class and the MyNamespace namespace.
Type myType = Type.forName('MyNamespace', 'ClassName');
public String getName()
Type: String
This example shows how to get a Type’s name. It first obtains a Type by calling forName, then calls getName on the Type object.
Type t = Type.forName('MyClassName'); String typeName = t.getName(); System.assertEquals('MyClassName', typeName);
public Integer hashCode()
Type: Integer
The returned hash code value corresponds to the type name hash code that String.hashCode returns.
public Object newInstance()
Type: Object
Because newInstance returns the generic object type, you should cast the return value to the type of the variable that will hold this value.
This method enables you to instantiate a Type that implements an interface and call its methods while letting someone else provide the methods’ implementation. For example, a package developer can provide an interface that a subscriber who installs the package can implement. The code in the package calls the subscriber's implementation of the interface methods by instantiating the subscriber’s Type.
Calling this method on a type corresponding to a class that has a private no-argument constructor results in a System.TypeException, as expected because the type can’t be instantiated. For Apex saved using Salesforce API version 28.0 and earlier, this method returns an instance of the class instead.
This example shows how to create an instance of a Type. It first gets a Type by calling forName with the name of a class (ShapeImpl), then calls newInstance on this Type object. The newObj instance is declared with the interface type (Shape) that the ShapeImpl class implements. The return value of the newInstance method is cast to the Shape type.
Type t =
Type.forName('ShapeImpl');
Shape newObj =
(Shape)t.newInstance();
public String toString()
Type: String
This method returns the same value as getName. String.valueOf and System.debug use this method to convert their Type argument into a String.
This example calls toString on the Type corresponding to a list of Integers.
Type t = List<Integer>.class; String s = t.toString(); System.assertEquals('List<Integer>', s);