Call: IN +91 7013283324 | +91 9347854179
(Click on WhatsApp Chat button)
Call: US +1 (415) 529-7271

Salesforce Apex Tutorial Chapter 12: Apex Classes

Home  >>  Salesforce Apex Tutorial  >>  Salesforce Apex Tutorial Chapter 12: Apex Classes

Salesforce Apex Tutorial Chapter 12: Apex Classes

On November 1, 2021, Posted by , In Salesforce Apex Tutorial, With Comments Off on Salesforce Apex Tutorial Chapter 12: Apex Classes
Apex-Classes
Apex classes are blueprints or templates from which objects can be created. Objects are instances of classes. Classes in Apex are similar to Java classes. In Apex, we can build top-level classes (also known as outer classes) as well as inner classes, i.e, classes defined inside another class.

Syntax:


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

Example:

public class ApexDemoController{ 
Public static void updateContact(){ 
// variable declaration
List<Contact> conList = new List<Contact>();
// SOQL query 
conList = [Select Id,FirstName,LastName from Contact Where LastName = 'Test'];
List<contact> listToUpdate = new List<Contact>();
// iteration over contact list
for(contact con:conList){
con.Title = 'Manager';
listToUpdate.add(con);
}
// flow control statement
if(listToUpdate != null || listToUpdate.size()> 0 ){
// DML statement
update listToUpdate;}}}

Let’s take a look at Apex Access Specifiers:

Following are the apex modifiers supported by the apex.

  • Private: This access specifier gives access to a class, method, variable to be used locally or within the section of code it is defined. All classes, methods, and variables that do not have any access specifier are private by default.
  • Public: Declaring a class ‘Public’ implies that it is accessible to your organization and within the defined namespace. It is generally used to define most of the Apex classes.
  • Protected: A method or variable defined with this access specifier in the Apex class is visible to any inner classes and to any classes that extend it. This access modifier is only applicable for instance methods and variables. 
  • Global: This access specifier gives access to a class, method, variable to be used by an apex within a namespace as well as outside of the namespace. It is a best practice not to use global keyword until necessary. It should be used for any method whose reference is required outside of the application, such as in SOAP APIs or Apex code. The class that contains a global method or variable must also be declared global.
Comments are closed.