SALESFORCE – VARIABLES APEX DEVELOPER GUIDE

SALESFORCE – APEX VARIABLES APEX DEVELOPER GUIDE

Hello, Friends, I am very happy to tell you all with this useful information. This post is only for Beginners. And for the experienced persons, this post helps them to refresh the things which they already know.

Variables:

Local variables are declared with Java-style syntax. As with Java, multiple variables can be declared and initialized in a single statement. Apex and Java are similar in many ways. Variable declaration in Apex and Java is also quite the same. Here I will discuss a few examples to understand how to declare local variables.
Integer i = 1;
String productName = ‘SFDC’;
Set<string> setOfStrings = new Set<string>();
Map<id, string> mapOfStringsIdToName = new Map<id, string>();

SALESFORCE – DATA TYPES APEX DEVELOPER GUIDE

NOTE: All the variables are assigned with the value null (By-Default). 

Declaring Variables

You can declare the variables in Apex like String and Integer as shown below−
Integer myInteger = 1;                      //Integer variable declaration
String strName = ‘My String’;     //String variable declaration
Boolean myBoolean = true;       //Boolean variable declaration

Apex variables are Case-Insensitive

This (Apex variables are Case-Insensitive) means that the code given below will throw an error since the variable ‘m’ has been declared two times and both will be treated as the same.

Integer m = 200;
for (Integer i = 0; i<10; i++) {
   integer m = 10; //This statement will throw an error as m is being declared
   again
   System.debug(‘This code will throw error’);
}

Scope of Variables

An Apex variable is valid from the point where it is declared in code. So it is not allowed to redefine the same variable again and in the code block. Also, if you declare any variable in a method, then that variable scope will be limited to that particular method only. However, class variables can be accessed throughout the class.

Example

//Declare variable String
List<string> String = new List<strings>();
String.add(‘SFDC’);
//You cannot declare this variable in this code clock or subcode block again
//If you do so then it will throw the error as the previous variable in scope
//Below statement will throw an error if declared in the same code block
List<string> String = new List<strings>();

SALESFORCE VISUALFORCE PAGE GUIDE FOR BIGINNER

Stay tuned to salesforceupdates.com for more updates.

Thank you so much for reading my post. Feel free to ask doubts in the comment section below, and I’ll come again with an interesting topic about Salesforce So Please Share, Comment, Like, Subscribe to our blog.

Leave a Reply