Playing with an Instance of Static 2


One concept that I found difficult to wrap my head around and even more difficult to explain was the difference between static and instance methods and variables. I had found some great resources along the way but found the best way to get a better understanding, was to play around with it a bit in my Developer Org. First, let’s take a look at a few of those resources:

  1. Static and Instance Methods, Variables, and Initialization Code
  2. Salesforce Stack Exchange Question / Answer
  3. Mr. Happy Object teaches static methods

Now let’s see it in action by creating the following HelloWorld class, which has both static and instance methods and variables:

Next, we’ll create a second class that will instantiate the Hello World class two times and make calls to both the instance methods along with the static methods. When we create the two instance methods, notice how we call the instance methods vs. the static methods; InstanceClassName.InstanceMethodName vs. ClassName.StaticMethodName. This is because the instance method belongs to the instance of the class, whereas, the static method belongs directly to the static class.

After the above code is saved, we will need to open the Execute Anonymous and execute the SayHello class by running the following line:

Now, let’s take a look at the resulting debug log:

DebugLog

For more fun with dick and jane, try editing the SayHello class and accessing the static method in the instanceOne object or accessing the instance method directly from the HelloWorld class. Take a look at the errors, they’ll help you in the future if you forget how to call the methods.


Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “Playing with an Instance of Static

  • Aaron Saray

    Cool article – one of the things I keep in mind when comparing the two is something like this:

    A static variable and a static method are something available to be used once, almost context-less. That is to say, once a static method is done being used, it kind of ‘resets’ back to how it was to begin with. An instance method is different – because it belongs to THIS copy (hence the this.method() type declarations) of the object.

    Generally (but not always), I view static methods as more utility methods and instance methods as more business related decisions.

    So with
    var book = new Book;
    book.setTitle(‘moby dick’);
    book.setPageCount(400);
    log (book.getDescription());

    LibraryService.checkoutBook(book);

    In this case, we create a book and set it to have properties – which – are repeatable properties for every new book – and those are accessed via instance methods – those methods creating values from this instance’s properties. Later on, we call a static method on a static class checkoutBook() – passing in a book instance to it.

    Who knows what happens – maybe it acts like a decorator and changes the values of the book (a checkout flag perhaps) – or maybe it takes that book and passes it to an external system that keeps that in a different data store – we don’t know – we don’t care what it did BEFORE – or what it’s going to do after. The important part of this transaction is that the book instance stays intact because that actually reflects something that exists.

    Hopefully that’s helpful and not just a ramble 🙂

    • JennyJBennett Post author

      Thanks Aaron! Most definitely helpful, another use case for static variables in Apex is to prevent recursive triggers since the static variable holds its value throughout the transaction.