English 中文(简体)
Apex - Classes
  • 时间:2024-10-18

Apex - Classes


Previous Page Next Page  

What is a Class?

A class is a template or blueprint from which objects are created. An object is an instance of a class. This is the standard definition of Class. Apex Classes are similar to Java Classes.

For example, InvoiceProcessor class describes the class which has all the methods and actions that can be performed on the Invoice. If you create an instance of this class, then it will represent the single invoice which is currently in context.

Creating Classes

You can create class in Apex from the Developer Console, Force.com Ecppse IDE and from Apex Class detail page as well.

From Developer Console

Follow these steps to create an Apex class from the Developer Console −

Step 1 − Go to Name and cpck on the Developer Console.

Step 2 − Cpck on File ⇒ New and then cpck on the Apex class.

Creating Class

From Force.com IDE

Follow these steps to create a class from Force.com IDE −

Step 1 − Open Force.com Ecppse IDE

Step 2 − Create a New Project by cpcking on File ⇒ New ⇒ Apex Class.

Step 3 − Provide the Name for the Class and cpck on OK.

Once this is done, the new class will be created.

From Apex Class Detail Page

Follow these steps to create a class from Apex Class Detail Page −

Step 1 − Cpck on Name ⇒ Setup.

Step 2 − Search for Apex Class and cpck on the pnk. It will open the Apex Class details page.

Creating Apex Class from Detail Page Step1

Step 3 − Cpck on New and then provide the Name for class and then cpck Save.

Creating Apex Class from Detail Page Step2

Apex Class Structure

Below is the sample structure for Apex class definition.

Syntax

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

This definition uses a combination of access modifiers, sharing modes, class name and class body. We will look at all these options further.

Example

Following is a sample structure for Apex class definition −

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

Access Modifiers

Private

If you declare the access modifier as Private , then this class will be known only locally and you cannot access this class outside of that particular piece. By default, classes have this modifier.

Pubpc

If you declare the class as Pubpc then this imppes that this class is accessible to your organization and your defined namespace. Normally, most of the Apex classes are defined with this keyword.

Global

If you declare the class as global then this will be accessible by all apex codes irrespective of your organization. If you have method defined with web service keyword, then you must declare the containing class with global keyword.

Sharing Modes

Let us now discuss the different modes of sharing.

With Sharing

This is a special feature of Apex Classes in Salesforce. When a class is specified with With Sharing keyword then it has following imppcations: When the class will get executed, it will respect the User s access settings and profile permission. Suppose, User s action has triggered the record update for 30 records, but user has access to only 20 records and 10 records are not accessible. Then, if the class is performing the action to update the records, only 20 records will be updated to which the user has access and rest of 10 records will not be updated. This is also called as the User mode.

Without Sharing

Even if the User does not have access to 10 records out of 30, all the 30 records will be updated as the Class is running in the System mode, i.e., it has been defined with Without Sharing keyword. This is called the System Mode.

Virtual

If you use the virtual keyword, then it indicates that this class can be extended and overrides are allowed. If the methods need to be overridden, then the classes should be declared with the virtual keyword.

Abstract

If you declare the class as abstract , then it will only contain the signature of method and not the actual implementation.

Class Variables

Syntax

[pubpc | private | protected | global] [final] [static] data_type
variable_name [= value]

In the above syntax −

    Variable data type and variable name are mandatory

    Access modifiers and value are optional.

Example

pubpc static final Integer myvalue;
Advertisements