Apex Classes and Key Entities

Irrespective of the language, class resembles a template or a typical blueprint that can be used for creating objects. An object is an instance of a particular class. This is true of any kind of object-oriented language, no matter it is C++ or Java, or even Apex. This is the standard definition of Class. Apex Classes are similar to Java Classes. We can cite the example of InvoiceProcessor class. This class describes the class that has got all the methods, along with the actions that can be performed on the Invoice. When we create an instance of this class, then this class will represent the single invoice that is currently in use.

How to create a new Apex class

In this section, we will learn how to create a simple Apex class on the Force.com platform. We can create a class in Apex from three different options:

  • Developer Console
  • Force.com Eclipse IDE
  • Apex Class detail page

From the developer console, we can follow the given steps to create an Apex class.

  • Click on Name and the click in the developer console.
  • Once the developer console launches, click on File -> New -> Apex Class.

Steps for creating Class from Force.com is given below:

  • Open Force.com Eclipse IDE
  • Create a New Project by clicking on File ⇒ New ⇒ Apex Class.
  • Provide the Name for the Class and then click on OK.

Similarly, we can follow these steps to create a class from the Apex Class Detail page.

  • Click on Name -> setup.
  • Search for 'Apex Class' and click on the link. That will lead to the Apex Class details page
  • In the next step, click on New, and then Save.

Structuring an Apex Class

An Apex class starts with its definition. Every Apex class is defined with the keyword “class”. An Apex class can be defined with the following syntax.

private | public | global [virtual | abstract | with sharing | without sharing]
class ClassName [implements InterfaceNameList] [extends ClassName] {
   // Classs Body
}

Here we can see four key entities:

  • Access modifiers
  • Sharing modes
  • Class name
  • Class body

A complete class definition would look like this.

public class MySampleApexClass {       //Class definition and body
   public static Integer myValue = 0;  //Class Member variable
   public static String myString = ''; //Class Member variable 
   public static Integer getCalculatedValue () {
   // Method definition and body
   // do some calculation
      myValue = myValue+10;
      return myValue;
   }
}

Access modifiers for Apex Class

Access modifiers define the access level within the class. We have three types of access modifiers in Apex. These are public, private, and global.

Private: If the access modifier is declared as 'Private', then this class will be local and this class cannot be accessed outside of that particular entity. By default, all the classes will have this modifier.

Public: If the access modifier is declared as 'Public' then this signifies that this class is accessible to the organization, along with the defined namespace.

Global: If the access modifier is declared as 'global' then this class will be accessible by all apex codes irrespective of the organization.

Sharing Modes in Apex

Sharing modes define the sharing rules for the class. In Apex, there are four modes of sharing. These are, with sharing, without sharing, virtual and abstract.

With Sharing: When a class has got the sharing mode as 'With Sharing' then when that particular class will get executed, it will consider the User's access settings and profile permission. This can be made more clear with an example, For example, a User's action has resulted in the update of records, however, the user has access to only 20 records, while there is no access for 10 records. Hence, in this, if the class is updating the records, it will update only 20 records where the access is there, and the rest of 10 records will not be updated where access is not there. This is also referred to as the User mode.

Without Sharing: Let us consider the example given in the last section. So, we will assume the user does not have access to the rest of the 10 records out of the 30 records, however, all the 30 records will be updated as the Class is getting executed in the system mode, which means that it has been defined with the "Without Sharing" keyword. This is also referred to as System Mode.

Virtual: With the 'virtual' keyword the virtual sharing mode can be enabled. In this sharing mode, the class can be extended, and also the overrides are allowed. It is suitable for the scenarios, where the methods are required to be overridden.