Salesforce Apex Class: An Overview of Apex

Similar to Java Programming, Apex Programming utilizes classes to execute specific tasks through programs. A class serves as a blueprint or structure from which objects are instantiated. Objects are essentially representations of the class. For instance, consider a class named “Student” that encompasses all the pertinent student details. Instances of the “Student” class could include the student’s ID or name. Within a class, methods and variables are contained. Variables define the object’s state, encompassing both its type and name, while methods dictate or manage the object’s behavior.

Defining a Class in Apex

Classes in Apex can be categorized as either inner or outer. When declaring a class, certain aspects need to be specified:

  • Access Modifier: An access modifier (such as public, protected, or private) should be utilized for both top-level and inner classes.
  • Modifier: Optionally, modifiers like abstract or virtual can be specified if needed.
  • “class” Keyword: The “class” keyword must precede the class name in the declaration.
  • If necessary, implementations or extensions can be employed within the class definition.


private class viswaouterclass{
Class mindinnerclass

Apex entities

Generally, objects are defined as instances of a class. In salesforce, objects are considered as classes or you can generate an object for sObject.  In classes, objects invoke the methods.

Instantiating a class:

Public class mindmajix

Integer i=1000;

Mindmajix objec1 = new Mindmajix();


Variables Declaration in Apex Class

In Apex classes, defining a variable involves specifying the following:

    • Access Modifiers: Specify any desired access modifier (e.g., public, static, final) before the variable.
    • Data Type: Every variable must be associated with a specific data type, such as Integer or Boolean.
    • Name: Each variable requires a unique name.
    • Value: Optionally, a value can be assigned to the variable as needed.

    Syntax for declaring a variable:

    The syntax for declaring a variable:

    Private | Public | Protected | [static] [final] [global] data_type variable_name = value;


    public global Boolean e = True;

    Functions within Apex Classes

    When declaring a method, the following components need to be specified:

    • Modifier: Optionally, use modifiers like protected or public for the method.
    • Data Type: If the method returns values, specify the data type for those values. For methods that don’t return anything, “Void” can be used as the data type.
    • Parameters: Methods might have parameters; if none are needed, empty parentheses are used.
    • Statements: The method’s code or statements should be enclosed within braces “{}”.


    Protected | Private | Public | [override] [static] [global] data_type method_name(parameters)
    private global float add( )
    return e;

    Apart from the library methods, users can define new methods depending on their requirement.


    A Constructor is defined as a code that is called while creating an object. If the class does not have a customized constructor, it can use built-in or public constructor. The syntax used for declaring a constructor is identical to the syntax of the method. Along with the constructor, we should use the “New” keyword for creating an object. Constructors are of two types; they are as follows:

    • Constructor with no arguments(Default Constructor)Constructor with arguments

    The new object can be created through constructor as follows:

    NewObject object1 = new NewObject()

    Overloading Constructors

    If a class has multiple constructors and with different arguments, then it is known as constructor overloading.

    Visibility Modifiers in Apex

    Within Apex Programming, the Access Modifiers available are as follows:

    • Public: Accessible to anyone within the program or namespace, allowing usage of public methods.
    • Private: Considered the Default Access Modifier, restricting access to methods within the declaring Apex class.
    • Protected: Accessible by the declaring inner class and outer classes extending that inner class.
    • Global: Methods declared as global can be accessed by those utilizing the class in which they’re declared.

    Characteristics in Apex

    In Apex, a Property resembles a variable, serving to validate data before alterations and track data modifications.

    A Property typically includes:

    • Access modifier: Any Access Modifier—such as Private, Public, Global, or Protected—can be assigned to a property.
    • Return_type: Each Property is associated with a return_type, like Boolean or Integer.
    • property_name: All properties must be assigned a name.

    Example of Apex Properties

    private class BaseProperty
     public Boolean prop1{
    get {return prop1; }
    set { prop1 = value; }

    Apex Methods

    Functions in Apex serve to manipulate data, perform calculations, handle records, or assign values to Visualforce properties. These functions are essential in Visualforce expressions for evaluation. Here are several functions commonly utilized in Visualforce pages:

    • ADDMONTHS: Generates data referencing a specific date by a certain number of months, either preceding or following the given date.
    • DATEVALUE: Generates a date value from a given time/data or the expression being tested.
    • DATETIME VALUE: Produces values encompassing the month, day, year, and GMT time.
    • DAY: Retrieves the day within the month, presented as a number ranging from 1 to 31.

    Extending a class

    Another class can extend a class, and this is called extending a class. For extending a class, we will use the “extends” keyword. The class which extends another class will use all the properties and methods of that class. Through the “override” keyword, the extended class can override the virtual methods present in the base class. 


    private Virtual Class Header {
    private virtual void read() {
    System.debug(‘ Read some content’);
    private virtual float average {
    read 98.5;

    Closing Thoughts

    In Salesforce, Apex classes serve to carry out an object’s actions, encompassing specific methods tailored for executing these actions. These classes include constructors aimed at object creation. Those familiar with Java programming can swiftly adapt to Apex classes.