Introduction to Class Variables & Methods and Objects
Class variables:
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
Syntax:
[public |private| protected |global| final] [static]Data - type variable – name
Ex:
Private static final integer My-INT;
Private finale integer I = 1;
Class methods:
To define a method, specify the following.1.optional: modifiers, such as public or protected.
2.Required: the data type of the value returned by the method, such as string or integer. Use void if the method does not return value.
3Required: A list of input parameters for the method, separated by common, each preceded by its data type, and enclosed in parentheses (). If
There are no parameters, use a set of empty parentheses.
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 here.
Syntax:
(public |private | protected | global) [over ride] [static]
Data-type method- name (input parameters)
{
// the body of the method.
}
Ex1:
Public static Integer get Int ()
{
return – MY INT ;
}
Ex2:
Public class example
{
Public Integer Show (Integer age)
{
System debug (‘My age is’ tag);
}
}
Object:
Object is an instance of the class. This as both state and behavior.
memory for the data members our allocated only when you create an object
Syntax:
Class name object name = New class name ();
↓ ↓ ↓
Object variable name constructor
Ex:
Class example
{
//code
}
Example e = new example ();
No comments:
Post a Comment