Newer Version Available
Type Class
Namespace
Usage
Example: Instantiating a Type Based on Its Name
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.
Class Property
The class property returns the System.Type of the type it is called on. It’s 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.
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:
Type Methods
The following are methods for Type.
equals(typeToCompare)
Signature
public Boolean equals(Object typeToCompare)
Parameters
- typeToCompare
- Type: Object
- The type to compare with the current type.
Return Value
Type: Boolean
Example
forName(fullyQualifiedName)
Signature
public static System.Type forName(String fullyQualifiedName)
Parameters
- fullyQualifiedName
- Type: String
- The fully qualified name of the class to get the type of. The fully qualified class name contains the namespace name, for example, MyNamespace.ClassName.
Return Value
Type: System.Type
Usage
forName(namespace, name)
Signature
public static System.Type forName(String namespace, String name)
Parameters
Return Value
Type: System.Type
Usage
Example
This example shows how to get the type that corresponds to the ClassName class and the MyNamespace namespace.
getName()
Signature
public String getName()
Return Value
Type: String
Example
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.
hashCode()
Signature
public Integer hashCode()
Return Value
Type: Integer
Usage
The returned hash code value corresponds to the type name hash code that String.hashCode returns.
isAssignableFrom(sourceType)
Signature
public Boolean isAssignableFrom(Type sourceType)
Parameters
- sourceType
- The type of the object with which you are checking compatibility.
Return Value
Type: Boolean
- childType.isAssignableFrom(parentType)
- typeA.isAssignableFrom(TypeB) where TypeB is a sibling of TypeA
- typeA.isAssignableFrom(TypeB) where TypeB and TypeA are unrelated
Usage
Unlike the instanceof operator, this method allows you to check type compatibility without having to create a class instance. This method eliminates static compile-time dependencies that instanceof requires.
Example
The following code snippet first defines sibling classes A and B that both implement the Callable interface and an unrelated class C. Then, it explores several type comparisons using isAssignableFrom().
newInstance()
Signature
public Object newInstance()
Return Value
Type: Object
Usage
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.
Example
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.
toString()
Signature
public String toString()
Return Value
Type: String
Usage
This method returns the same value as getName. String.valueOf and System.debug use this method to convert their Type argument into a String.
Example
This example calls toString on the Type corresponding to a list of Integers.