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?

  • sfdcfox

    sfdcfox Correct answer

    7 years ago

    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.

    @minige, an example place in the doc where it is used is the example for `JSON.deserializeUntyped()`

    @cropredy can you give an exmaple?

    @Mann -- if you have a new question, please use the ask question link

    @cropredy next time i will. i found wat i was looking for. thanks

    `Object` Also supports `clone()` :)

    if(source instanceof integer) IS A SAVIOR. It's amazingly useful for overloading. Thank you sfdcfox! https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_instanceof.htm

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used