Governor Limits · Salesforce Basic

Apex Class, Variables, Constructor & Methods in Salesforce.

If you know Java or Dotnet applications, then you may observe that everything is written as a class. There are different access levels. You can also define class within a class just like inner class and outer class etc.

Salesforce also provides similar syntax that is followed in Java. Syntax can be to defining a class or variables in class or a wrapper class everything is similar to the syntax followed in Java programming.

Let us see a class and things that can be done in a class in Salesforce.

public class A_Class_Name{ 

        // Variables
	public static string a_Variable {get;set;}

        // Constructor
	public A_Class_Name(){
	        // Constructor business logic code
        }

        // Methods 
        public void a_Method_Name(Integer a_Variable1, Ingeter a_Variable2){
	        // Business logic code
        }
}

We can do the following in a class.

  1. Defining Class
  2. Declaring variables
  3. Constructor of class
  4. Defining methods in class.

All above syntax have one in common, and that is access modifiers. Access modifiers can be public, private, protected and global.

Know more about access modifiers.


Defining Class

Syntax:

Access_Modifier class A_Class_Name{

        // A class with name "A_Class_Name"
}

There are 3 parts in defining a class namely, access modifiers, class keyword, class name. This is how its is defined in Java as well. But here in salesforce there is one more key word that can be defined and that is “with sharing” and “without sharing”. By default, if user do not define anything, then it will be without sharing.

Example:

/*
 * Access Modifier: public
 * Class Name: A_Class_Name_A
 * Description : This is a example class to demonstrate with sharing access
 * Author: Author_Name.
 */
public with sharing class A_Class_Name_A{
	// This is with sharing class (User mode)
        // A public class which has with Sharing access
        // and has name "A_Class_Name_A"
}

/*
 * Access Modifier: public
 * Class Name: A_Class_Name_B
 * Description : This is a example class to demonstrate without sharing access
 * Author: Author_Name.
 */
public without sharing class A_Class_Name_B{
	// This is without sharing class (System mode)
        // A public class which has without Sharing access
        // and has name "A_Class_Name_B"
} 

/*
 * Access Modifier: public
 * Class Name: A_Class_Name_C
 * Description : This is a example class to demonstrate without sharing access
 * Author: Author_Name.
 */
public class A_Class_Name_C{
	// This is without sharing class (System mode)
	// By default class will be without sharing.
        // A public class which has without Sharing access
        // and has name "A_Class_Name_C"
}

Note:

  • There are governor limits in salesforce for apex code also.
  • Number of characters for a class in 1 million.
  • Maximum amount of code used by all Apex code in an org is 6MB.

Know more about System mode and User mode.


Declaring Variable

Declaring variable in salesforce is similar to the one in Java. You can declare null variables and variables with some values. All variables will have one or other data type, such as sObject, Primitive or Enum.

Syntax:

Access_Modifier  Data_Type   a_Variable_Name; 

Access modifier is optional because, variables can be declared locally or within class and can also be used as a reference outside class as well.

Date_Type can be primitive datatype or Sobject or enum.

a_Variable_Name can be any variable name required as per business logic.

Example:

// a variable of type String
public string a_New_String;
 

// a variable of type Integer
private integer a_New_Integer;

Note:

  • A variable can be static also.
  • Variables can be declared within scope and can be used locally.
  • Variable name should be unique within scope.
  • An uninitialized boolean variable is initialized to false by the system.
  • Multiple variables can be declared and initialized in a single statement, using comma separation.
    • Ex: Integer a,b,c;
  • If you declare a variable and don’t initialize it with a value, it will be null.
  • null means the absence of a value.
  • You can also assign null to any variable declared with a primitive type.
  • All data types, Objects, Classes will have some default methods defined by the system.
    • Ex: valueOf(), size(), Length().
  • Apex is also case-insensitive. Meaning Integer A and Integer a both are same. And will throw error.
  • You can define a constant variable by using key word ‘final’.

Constructor of class

Constructor is the first method that is executed when a class object is created. Constructor can be with attribute and without attribute. You need not write a constructor code if not required. System by default created a default constructor without attribute(No argument default constructor).

  • Constructor name will always be same as the name of class.
  • Constructor will not have a return type.

Syntax:

Access_Modifier   class   A_Class_Name{

        // A constructor.
	Access_Modifier   A_Class_Name(){
		// Constructor code.
	}

}

Constructor can be with argument or without argument as shown below. Below mentioned examples is also called as Constructor Overloading.

Example:

/*
 * Access Modifier: public
 * Class Name: A_Class_Name
 * Description : This is a example class to demonstrate how constructor can be defined.
 * Author: Author_Name.
 */
public class A_Class_Name{

        /*
         * Access Modifier: public

         * Description: This is a constructor without argument.
         * Arguments: None.
         * Author: Author_Name.
         */
	public A_Class_Name(){ 

	        // Without argument (No argument constructor)
		// Constructor code.
	}
	
        /*
         * Access Modifier: public

         * Description: This is a constructor with argument.
         * Arguments: 1
         ******** 1) a_Variable - String type - <Reason>
         * Author: Author_Name.
         */
        public A_Class_Name(String a_Variable){ 
		// Constructor code.
                // Argument "a_Variable" can be of any data type.
                // In this example it is of type String.
	}

        /*
         * Access Modifier: public

         * Description: This is a constructor with argument.
         * Arguments: 2
         ******** 1) a_Variable - String type - <Reason>
         ******** 2) a_Flag - Boolean type - <Reason>
         * Author: Author_Name.
         */
        public A_Class_Name(String a_Variable, Boolean a_Flag){ 
		// Constructor code.
 
                // Argument "a_Variable" and "a_Flag" can be of any data type.
                // In this example it is of type String and Boolean.
	}

}

Note:

  • If you write a constructor with argument, then you can use the constructor to create a object using those arguments.
  • If you create a constructor that takes arguments, and you still want to use a no-argument constructor, you must create your own no-argument constructor in your code.
  • Once you create a constructor for a class, you no longer have access to the default, no-argument public constructor.
  • You can create overloaded constructor, which means more than one constructor with different parameters.
  • In visualforce page, if a controller is mentioned then no argument constructor of that class will execute when a page is loaded.
  • Constructor will not have any return type.
  • Cannot call @future annotation methods from constructor.

A new object of the above class type can be created as follows.

/*
 * An object "a_New_Object1" will be create and 
 * no argument constructor will be executed.
 */
A_Class_Name a_New_Object1 = new A_Class_Name();


/* 
 * An object "a_New_Object2" will be create and 
 * a constructor will be executed which takes parameter as string.
 * "a_Variable" will hold 'Test String Value'
 */
A_Class_Name a_New_Object2 = new A_Class_Name('Test String Value');



/* 
 * An object "a_New_Object3" will be create and 
 * a constructor will be executed which takes parameter as String and Boolean.
 * "a_Variable" will hold 'Test String Value'
 * "a_Flag" will hold 'True' as Boolean value.
 */
A_Class_Name a_New_Object3 = new A_Class_Name(‘Test String Value’, True);

Defining Method in Class.

When you want to execute a business logic, then you can define a method in a class and call that method as and when required.

Syntax:

Access_Modifier  Return_Type  a_Method_Name(Date_Type  a_Attribute1, Date_Type  a_Attribute2, ...){
	
        // Method code.
	// Business logic.
	
}

Defining a method has multiple parts, an access modifier, a return type, followed by method name. defining attribute is optional.

Access modifier can be public, private, protected and global. Default access modifier is private.

All methods will have a return type, it can be any primitive data type or sObject.

You can give a method name which gives proper name for the logic written in the method.

Attributes are optional. By defining attributes, you can pass input value to the method which are required to execute the business logic. If any business logic requires some input values, then define attribute, if business logic do not require any input you need not define attributes at all and leave it blank.

Example:

/*
 * Method Name : m_Add
 * Access Modifier: public
 * Return Type: Integer
 * Attributes : 2
 ******* 1) a_Value1
 - Integer type
 ******* 2) a_Value2 - Integer Type
 */


public Integer m_Add(Integer a_Value1, Integer a_Value2){
	integer a_Result;
	a_Result = a_Value1 + a_Value2;
	return a_Result;
}

Once the method is written, you can call the method as shown in below example. Let us consider that the class name in which this method is written is A_Calculator.

// Create a Object of the class type.
A_Calculator a_Object = new A_Calculator();

//Call the method name as, Object.MethodName();
Integer a_Result = a_Object.m_Add(5,4);

// Check the value using assert Statement.
System.assertEquals(9,9);

Note:

  • A method with void as return type need not write a return statement at end of method.
  • A method which returns a return type value, then at end of method return statement should be written such that it returns same data type value. Ex: is return type is String, then at end of method a string variable should be returned.
  • Methods can be static.
  • There is governor limit in writing a method as well.
  • Method size limit is 65,535 bytecode instructions in compiled form.

Resource:

Salesforce: Using Constructors

Akhil Kulkarni – Governor Limits & Solutions

Salesforce: System Class

14 thoughts on “Apex Class, Variables, Constructor & Methods in Salesforce.

  1. Hey! Someone in my Facebook group shared this site with us so I came
    to take a look. I’m definitely enjoying the information. I’m bookmarking and will be
    tweeting this to my followers! Exceptional blog and amazing design.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s