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.
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. |
Classes can be inner and outer. To declare a class, we should mention the following:
Example
private class viswaouterclass{
Statement1
Class mindinnerclass
{
Statement2
}
}
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:
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;
To declare a method, we have to mention the following fields:
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.
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:
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.
In Apex Programming, we will have the following Access Modifiers:
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:
Example of Apex Properties
private class BaseProperty
{
public Boolean prop1{
get {return prop1; }
set { prop1 = value; }
}
}
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:
For more details visit - Salesforce Apex Tutorial |
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! |
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:
Name | Dates | |
---|---|---|
Salesforce Training | Nov 19 to Dec 04 | View Details |
Salesforce Training | Nov 23 to Dec 08 | View Details |
Salesforce Training | Nov 26 to Dec 11 | View Details |
Salesforce Training | Nov 30 to Dec 15 | View Details |
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.