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.
- Optional : Modifiers such as public or final as well as static.
- Required : The data type of the variable, such as String or Boolean.
- Optional : The value of the variable.
- Optional : The name of the variable.
</>
Copy
[public | private | protected | global | final] [static] data_type Variable_name
Example
</>
Copy
private Static final Integer My_INT;
private final Integer i=5;
Class Methods
To define a method specify the following
- 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.
- 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.
- A method can only have 32 input parameters.
- Required : The body of the method, enclosed in braces { }. All the code for the method, including any local variable declarations is contained base.
Syntax
</>
Copy
(public | private | protected | global ) [Override] [static] data_type Variable_name
(input parameters)
Example
</>
Copy
public Static Integer getInt() {
return My-INT;
}
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
</>
Copy
Classname objectname = new Classname();
Example
</>
Copy
Class example {
\\code
}
Example e = new Example();