
Salesforce Apex Tutorial Chapter 12: Apex Classes

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.
Comments are closed.