Deep Dive into Static Context in Visualforce Apex Controllers!

Apex static is always confusing for me. I am from a Java background, so it took a while for me to really understand the differences between Apex and Java static. This post discusses apex controller static behavior, i.e., in relation to Visualforce only. Here by Apex Controller static, I mean static variables/blocks declared in the controller associated with a Visualforce page.

The Apex STATIC Context—Variables and Code Initialization Blocks!

(Intermediate/Advanced Apex developers can skip this section.)

Apex gives static context in the form of static variables and static blocks. Most developers from a Java background understand the static block, but those who are ramping up from PHP, etc., please go through this topic on static in this Salesforce guide.

Apex VF Controller Static: Common Myths!

Developers, especially from a Java background, assume that static variables, once initialized on first-class load, can be shared/updated across multiple HTTP requests. This typically goes with Java classes. But this is not true with Apex; never expect to set some value in the Apex static variable and expect it back in the next request, even the next Ajax request. On every new request, you will find static variables re-initialized to the value declared or updated in static blocks.

Then, what is STATIC in Apex Controller?—Real Story!

In general, as per the Salesforce document, this is the definition of static.

Static variables are only static within the scope of the request. They are not static across the server, or across the entire organization.
— Salesforce Developer Guide

This definition is a one-liner, but it applies well to every Apex static context. The thumb rule to understand static in Apex controllers is:

Static Variables and Blocks are re-executed on each User HTTP Request or Visualforce Ajax Request
— Salesforce Developer Guide

Please NOTE that the static variables/blocks are re-executed for Ajax requests generated via Visualforce, i.e., by actionFunction, actionPoller, commandLink, or commandButton tags, etc. You must be wondering why. This is because HTTP is a stateless protocol; Visualforce only maintains the view state (as a hidden input field) to re-store all non-transient instance-level variables in Apex Controller. Static variables are never stored in the view state, as stated clearly in this guide:

Static variables aren’t transmitted as part of the view state for a Visualforce page.
— Salesforce Developer Guide

So to re-store all static stuff back, the force.com platform recreates all static variables and executes all static blocks on each normal or Ajax HTTP request. So that is why you can’t share data across the requests via static variables; they are always recreated first on each request (VF Ajax request too).

Apex Static and Change in Mindset

For programmers from other backgrounds like J2EE, PHP, etc., it’s a mindset change because:

  • JVM (Java Virtual Machine) usually doesn’t reload classes. A Java class is loaded once in memory and usually stays till the container is UP. The same stands true with static variables in Java too; all information kept in a static context is available throughout the application across different user sessions.

  • J2EE & Frameworks like Struts, create a session per user login like Salesforce. But that session is available to multiple requests via cookies and acts as a good mini-container to store logged-in user-specific information.

But both Java approaches are memory hogs and not scalable to multiple users; that’s why J2EE containers have complex sessions and other clustering mechanisms to scale for huge enterprise apps.

So, having static in this manner is important for the success of the multitenant nature of the force.com platform. As force.com is multitenant, multiple customer orgs, with multiple users share the same hardware resources like CPU and RAM (memory). If Apex static was like Java, then I can’t imagine how much memory would be required by force.com servers to match the current load. I am sure they won’t scale!

So, are Static Variables good for anything in Apex Controllers?

Static variables are not part of the view state. One can consider static as “C” language CONST and use them to declare constants in Apex.

Next…

In my next post, I will discuss Apex static context in Triggers. Would love to discuss your ideas/views on the Apex Controller static context.

Resources:

Let’s Talk

CTA Banner - Contact Form
Abhinav Gupta

First Indian Salesforce MVP, rewarded Eight times in a row, has been blogging about Salesforce, Cloud, AI, & Web3 since 2011. Founded 1st Salesforce Dreamin event in India, called “Jaipur Dev Fest”. A seasoned speaker at Dreamforce, Dreamin events, & local meets. Author of many popular GitHub repos featured in official Salesforce blogs, newsletters, and books.

https://abhinav.fyi
Previous
Previous

Editing Custom Labels in Force.com IDE !

Next
Next

Working with Aggregate SOQL queries/results in Batch Apex!