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; it can be the type or name of the object. Methods are used to define or control the behaviour of the object.

Declaring a Class

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.

Want to enhance your skills in dealing with the worlds best CRM, enroll in our Salesforce Training 

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

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

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

Example:

public global Boolean e = True;

Check Out Salesforce Tutorials

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 data type for those values. If the method does not return a value, we can use “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;

}

Subscribe to our youtube channel to get new updates..!

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

Constructor

A Constructor is defined as a code which 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 Programming, we will have the following Access Modifiers:

1. Public

Anyone in the program or namespace can use public Method.

2. Private

This access modifier is known as the Default Access Modifier. Private methods are accessed by the Apex class in which it is declared.

3. 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.

4. 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:

private class BaseProperty

{

 public Boolean prop1{

get {return prop1; }

set { prop1 = value; }

}

}

Checkout Salesforce Interview Questions

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:

1. ADDMONTHS

It yields the data that refers to the number of months after or before a particular date.

2. DATEVALUE

It yields a date value from time/data or expression under test.

3. DATETIMEVALUE

It yields month, day, year and GMT time value.

4. DAY

It yields a day present in the month in the type of a number between 1 and 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. 

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 about “How to call APEX Class”. Keep following us for more info on Salesforce Development / Programming.

Are you looking to get trained on Salesforce, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our Salesforce Certification Training program from your nearest city.

Salesforce Training Chennai, Salesforce Training Dallas, Salesforce Training Bangalore, Salesforce Training Hyderabad, Salesforce Training Mumbai, Salesforce Training Delhi, Salesforce Training Noida, Salesforce Training New York, Salesforce Training Chicago, Salesforce Training Kolkata, Salesforce Training Gurgaon, Salesforce Training Pune.

These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified Salesforce Trainers

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

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts