Hurry! 20% Off Ends SoonRegister Now

Apex Class in Salesforce

Apex is an Object-oriented programming language that runs on the Force.com platform. A class is a model or blueprint for the creation of objects. Here you will find the detailed information on the Apex class in Salesforce

In this article, we have enlightened the essential aspects of Apex objects, properties, Modifiers etc., which will help you understand the Salesforce Apex class.

Salesforce Apex Class: Apex Introduction

Like Java Programming, Apex Programming also has classes to do a particular task through programs. Class is a model or a plan by which objects are generated. Objects are considered as instances of the class. For example, if we take a class called “Student”, it explains all the details of the student. The instance of the “Student” class can be id or Name of the student. The class comprises methods and variables. Variables are used to define the state of an object; they can be the type or name of the object. Methods are used to define or control the behaviour of the object.

Want to enhance your skills in dealing with the world's best CRM, enroll in our Online Salesforce Training.

Declaring a Class in Apex

Classes can be inner and outer. To declare a class, we should mention the following:

  1. Access Modifier: we should use any modifier(public, protected, private) for the top-level class or inner class.
  2. Modifier: If we require, we can specify modifiers like abstract, virtual, etc.
  3. “Class” Keyword: “Class” Keyword should be declared before the name of the class.
  4. If we require, we can use implementations or extensions in the class.

Example

private class viswaouterclass{
Statement1
Class mindinnerclass
{
Statement2
}
}

Apex objects

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.

Object creation for a class:

Public class mindmajix

Integer i=1000;

Mindmajix objec1 = new Mindmajix();

objec1.umethod(1000);

Variables Declaration in Apex Class

In an Apex class, for declaring a variable, we have to define the following:

  • Access Modifiers: If we want an access modifier for a variable, we have to specify it before the variable. Modifiers can be as public, static, final, etc
  • Data Type: For every variable, we should mention a data type with it. The data type can be Integer or Boolean.
  • Name: Every variable should have a name.
  • Value: If required, we can store a value in the variable.

The syntax for declaring a variable:

The syntax for declaring a variable:

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

Example:

public global Boolean e = True;

Methods in Apex Class

To declare a method, we have to mention the following fields:

  • Modifier: If we require, we can use modifiers like protected, public for the method.
  • Data Type: As the method returns certain values, we should mention the data type for those values. If the method does not return a value, we can use the “Void” data type for it.
  • Parameters: Methods may or may not have parameters. If the method does not have parameters, we will have blank parenthesis.
  • Statements: The statements or code associated with a method, should be contained in the braces “{}”.

Syntax:

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

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

Constructor

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()

Constructor Overloading

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

Access Modifiers in Apex

In Apex Programming, we will have the following Access Modifiers:

  • Public: Anyone in the program or namespace can use public Method.
  • Private: This access modifier is known as the Default Access Modifier. Private methods are accessed by the Apex class in which it is declared.
  • Protected: The protected method can be used by the inner class in which it is declared and the outer classes which extend that inner class.
  • Global: Global Methods are used by those who can use the class in which they are declared.

Apex Properties

An Apex Property is identical to a variable. Properties are used to verify the data before the modifications are done. They are also used to indicate the changes that happened in the data. 

A Property can have the following:

  • Access modifier: All the Access Modifiers like Private, Public, Global, Protected can be applied to a property. 
  • Return_type: Every Property will have return_type like Boolean, Integer, etc.
  • property_name: Every property should have a name.

Example of Apex Properties

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

Apex Functions

They are used to alter data from implementing calculations, records, or to give values to visualforce properties. To get evaluated, functions are used in visualforce expression. The following functions are used in the visualforce pages, they are as follows:

  • ADDMONTHS: It yields the data that refers to the number of months after or before a particular date.
  • DATEVALUE: It yields a date value from time/data or expression under test.
  • DATETIME VALUE: It yields month, day, year and GMT time value.
  • DAY: It yields a day present in the month in the type of a number between 1 and 31.
For more details visit - Salesforce Apex Tutorial

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. 

Example:

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

Conclusion

In Salesforce, Apex classes are used to implement the actions associated with an object. Apex classes will have specific methods to execute the actions of the object. classes will have constructors to create the objects. Individuals who have knowledge of java programming can quickly get a grip over Apex classes.

In the next topic, we will discuss in detail “How to call APEX Class”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!

 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Salesforce TrainingDec 24 to Jan 08View Details
Salesforce TrainingDec 28 to Jan 12View Details
Salesforce TrainingDec 31 to Jan 15View Details
Salesforce TrainingJan 04 to Jan 19View Details
Last updated: 03 Apr 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles