Apex · Salesforce

DIFFERENCE BETWEEN VARIABLE AND CONSTANT IN SALESFORCE APEX

Variable :-

Variable value can be change any time. it cant be fix.

How to declaring Variables :-

You can declare the variables in Apex like String and Integer as follows −

String strName = 'My String';  //String variable declaration
Integer myInteger = 1;         //Integer variable declaration
Boolean mtBoolean = true;      //Boolean variable declaration

Apex variables are Case-Insensitive :-

This means that the code given below will throw an error since the variable ‘a’ has been declared two times and both will be treated as the same.

Integer a = 100;
for (Integer i = 0; i<10; i++) {
   integer a = 1; //This statement will throw an error as a is being declared
   again
   System.debug('This code will throw error');
}
Constant :-

Constant value can not be change. As in any other programming language, Constants are the variables which do not change their value once declared or assigned a value.

Constants can be defined using the final keyword, which means that the variable can be assigned at most once, either in the declaration itself, or with a static initializer method if the constant is defined in a class.

static final Integer fstNumber=10;
(OR)
static final Integer fstNumber;
static{
  fstNumber=10;
}

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s