24

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?

28

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) {
        return;
    }
    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.

  • 1
    @minige, an example place in the doc where it is used is the example for JSON.deserializeUntyped() – cropredy Jul 8 '14 at 18:10
  • @cropredy can you give an exmaple? – datmannz Oct 25 '16 at 6:36
  • @Mann -- if you have a new question, please use the ask question link – cropredy Oct 25 '16 at 16:56
  • @cropredy next time i will. i found wat i was looking for. thanks – datmannz Oct 26 '16 at 6:57
  • 2
    Object Also supports clone() :) – eyescream Mar 22 '17 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.