Classes and Object in Apex

11 minute read
0
Classes and Object in Apex
Salesforce Class:

A class is a template or blueprint from which objects are created. An object is an instance of a class. A class can contain variables and methods. Variables specify an object's state, such as the object’s Name or Type. Since these variables are associated with a class and are members of it, they are commonly referred to as member variables.

  • In Apex, a Salesforce class refers to a custom class that you create to define the behavior and properties of a specific object or to implement custom business logic.
  • Salesforce classes are written in Apex programming language and are used to extend the functionality of the Salesforce platform.
  • They can contain variables, methods, constructors, and properties to define the behavior and structure of the class.
  • Salesforce classes can be categorized into two main types: standard Salesforce classes and custom classes.
  • Standard Salesforce classes are pre-defined by Salesforce and offer various functionalities to interact with the platform's standard objects, perform database operations, handle security, implement triggers, and more.
  • Custom classes are classes that you create to define your own custom objects or implement custom business logic specific to your organization's requirements.
Syntax to declare a class:
 class <class_name> {   
   field;   
   method;   
 }  

To define a class, specify the following:

  1. Access modifiers:
  • You must use one of the access modifiers (such as public or global) in the declaration of a top-level class.
  • You do not have to use an access modifier in the declaration of an inner class.
  1. Optional definition modifiers (such as virtual, abstract, and so on)
  2. Required: The keyword class followed by the name of the class
  3. Optional extensions and/or implementations

Example 1:

Public | Global class Demo {
         public Integer age;
         public String name; 
}

Example 2:

Public class Employee{
        Public String empName;
        Public Decimal Salary;
        Public Integer Exp;
}

Syntax (with optional keywords in apex)

private | public | global [virtual | abstract | with sharing | without sharing] 
      class ClassName [implements InterfaceNameList] [extends ClassName] { 
   // The body of the class
}

 

Private This keyword can only be used with inner classes.

The private access modifier declares that this class is only known locally, that is, only by this section of code.

Public The public access modifier declares that this class is visible in your application or namespace.
Global The global access modifier declares that this class is known by all Apex code everywhere.
With Sharing / Without Sharing The with sharing and without sharing keywords specify the sharing mode for this class.

The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class

Virtual The virtual definition modifier declares that this class allows extension and overrides.

You cannot override a method with the override keyword unless the class has been defined as virtual.

Abstract The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined.

An object is an instance of a class, which means an object is a real time implementation of a class.It is assigned to a reference variable that is used to access all of the instance’s properties and methods. When you make a new instance the process is called instantiation and is typically done using the new keyword.

Creating object for the Employee class (refer class examples above )

Employee E1 = new Employee ();

 

Statements Purpose
new Employee() This allocates memory and takes copy of class variables
E1 This variable is of type class “Employee” takes the reference of the object created using keyword “new” and constructor.

Using E1, now we can access class data members and methods using referencevariable dot memeber name.

E1.empName = ‘Ranjith’;
E1.salary = 10000;
System.debug(‘employee Name ’ + E.Name);

System.debug() : debug() is the static method belongs to inbuilt class called “system” used to write the specified message, in string format, to the execution debug log.

Object Creation from Class

You can create an object of class as you might have done in Java or other object-oriented programming language.

Following is an example Class called MyClass −

// Sample Class Example
public class MyClass {
   Integer myInteger = 10;
   
   public void myMethod (Integer multiplier) {
      Integer multiplicationResult;
      multiplicationResult = multiplier*myInteger;
      System.debug('Multiplication is '+multiplicationResult);
   }
}

This is an instance class, i.e., to call or access the variables or methods of this class, you must create an instance of this class and then you can perform all the operations.

// Object Creation
// Creating an object of class
MyClass objClass = new MyClass();

// Calling Class method using Class instance
objClass.myMethod(100);

sObject creation

sObjects are the objects of Salesforce in which you store the data. For example, Account, Contact, etc., are custom objects. You can create object instances of these sObjects.

Following is an example of sObject initialization and shows how you can access the field of that particular object using dot notation and assign the values to fields.

// Execute the below code in Developer console by simply pasting it
// Standard Object Initialization for Account sObject
Account objAccount = new Account(); // Object initialization
objAccount.Name = 'Testr Account'; // Assigning the value to field Name of Account
objAccount.Description = 'Test Account';
insert objAccount; // Creating record using DML
System.debug('Records Has been created '+objAccount);

// Custom sObject initialization and assignment of values to field
APEX_Customer_c objCustomer = new APEX_Customer_c ();
objCustomer.Name = 'ABC Customer';
objCustomer.APEX_Customer_Decscription_c = 'Test Description';
insert objCustomer;
System.debug('Records Has been created '+objCustomer);

Static Initialization

Static methods and variables are initialized only once when a class is loaded. Static variables are not transmitted as part of the view state for a Visualforce page.

Following is an example of Static method as well as Static variable.

// Sample Class Example with Static Method
public class MyStaticClass {
   Static Integer myInteger = 10;
   
   public static void myMethod (Integer multiplier) {
      Integer multiplicationResult;
      multiplicationResult = multiplier * myInteger;
      System.debug('Multiplication is '+multiplicationResult);
   }
}

// Calling the Class Method using Class Name and not using the instance object
MyStaticClass.myMethod(100);

Static Variable Use

Static variables will be instantiated only once when class is loaded and this phenomenon can be used to avoid the trigger recursion. Static variable value will be same within the same execution context and any class, trigger or code which is executing can refer to it and prevent the recursion.

Post a Comment

0Comments
Post a Comment (0)