Apex-クラス

クラスとは何ですか?

クラスは、オブジェクトの作成元となるテンプレートまたは青写真です。オブジェクトはクラスのインスタンスです。これはクラスの標準的な定義です。ApexクラスはJavaクラスに似ています。

例えば、 InvoiceProcessorclassは、請求書で実行できるすべてのメソッドとアクションを含むクラスを記述します。このクラスのインスタンスを作成すると、現在コンテキストにある単一の請求書が表されます。

クラスの作成

Apexでクラスを作成するには、開発者コンソール、Force.com Eclipse IDE、およびApexクラスの詳細ページからも使用できます。

開発者コンソールから

次の手順に従って、開発者コンソールからApexクラスを作成します-

Step 1 − [名前]に移動し、開発者コンソールをクリックします。

Step 2 − [ファイル]⇒[新規]をクリックしてから、Apexクラスをクリックします。

Force.comIDEから

次の手順に従って、Force.comIDEからクラスを作成します-

Step 1 − Force.com EclipseIDEを開きます

Step 2 − [ファイル]⇒[新規]⇒[Apexクラス]をクリックして、新しいプロジェクトを作成します。

Step 3 −クラスの名前を入力し、[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 − Click on Name ⇒ Setup.

Step 2 − Search for 'Apex Class' and click on the link. It will open the Apex Class details page.

Step 3 − Click on 'New' and then provide the Name for class and then click Save.

Apex Class Structure

Below is the sample structure for Apex class definition.

Syntax

private | public | 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 −

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

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.

Public

If you declare the class as 'Public' then this implies 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 implications: 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

[public | 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

public static final Integer myvalue;