Apex Class Variables

In this Salesforce tutorial, we will learn about Apex Class Variables, class methods and objects.

The variables in the class should specify the following properties when they are defined.

  1. Optional : Modifiers such as public or final as well as static.
  2. Required : The data type of the variable, such as String or Boolean.
  3. Optional : The value of the variable.
  4. Optional : The name of the variable.
[public | private | protected | global | final] [static] data_type Variable_name

Example

private Static final Integer My_INT;
private final Integer i=5;

Class Methods

To define a method specify the following

  1. Optional : The data type of the value returned by the method, Such as String or Integer use void if the method does not return a value.
  2. Required : A list of input parameter for the method separated by commas, each preceded by  its data type, and enclosed in parentheses(). If there are not parameters, use a set of empty parentheses.
  3. A method can only have 32 input parameters.
  4. Required : The body of the method, enclosed in braces { }. All the code for the method, including any local variable declarations is contained base.

Syntax

(public | private | protected | global ) [Override] [static] data_type Variable_name
(input parameters)

Example

public Static Integer getInt() {
   return My-INT;
 }
ADVERTISEMENT

Object

Object is a instance of a class. This has both State and behaviour. Memory for the data members are allocated only when you create a object.

Syntax

Classname objectname = new Classname();

Example

Class example {
 \\code
}

Example e = new Example();