What are classes in Apex programming?

What are classes in Apex programming?

On January 19, 2024, Posted by , In Salesforce Apex Tutorial, With Comments Off on What are classes in Apex programming?
Classes in Salesforce Apex
Classes in Salesforce Apex

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.

CRS Info Solutions offers a comprehensive and dynamic Salesforce online course career building program for beginners, covering admin, developer, and LWC concepts. This course features immersive real-time projects, interactive hands-on learning, detailed daily notes, essential interview questions, thorough certification preparation, and strategic job prep guidance. Join their inspiring free demo to embark on an exciting Salesforce journey with expert mentorship and unlock your full potential in the Salesforce ecosystem. Enroll for a free demo today!

Next chapter Methods in Salesforce Apex.

Comments are closed.