[SalesForce] Object class in Apex

I'd like to know if there is an idea of very base Object class in Apex, like we have it in Java or C#. Seem like it's supported, for example I can write something like the following in developer console and it works:

List<Object> test = new List<Object> {1, 'a'};

But I can't find any documentation about it. Does anyone know if it's officially supported?

Best Answer

There is a system class called Object that is the base for every other construct in the system. This class is completely useless except as a "void pointer" to other types of data. For example, we can store any type of data in an object, and we can use instanceof to retrieve the base type of the object.

static void dosomething(object source) {
    if(source == null) {
    if(source instanceof integer) {
    if(source instanceof map<string, integer>) {
    ... etc

It appears to support two functions: hashcode() and equals(object). However, calling either method appears to crash the code and/or cause unexpected results (but they do compile and run). It is safe to use the standard equality operators, ==, !==, ===, and !==. You can safely use Object as a key or value in a map, set, or list. You cannot construct an object, as it is most likely defined as abstract.

There is no documentation on this class that I've ever seen; it's mentioned in various parts of the documentation, but only ever as a placeholder for generic type interfaces. Note that the compiler internally refers to this type as ANY. It's only as functional as it needs to be to allow polymorphic code.

Related Topic