Declaring variables in Apex is similar to Java. Below are 4 examples:

String productName = 'HCL';
Integer i = 0;
Set<string> setOfProducts = new Set<string>();
Map<id, string> mapOfProductIdToName = new Map<id, string>();

You can see that variables can be varying levels of complexity. Here we run the gamut from primitives to collections.

There are syntactic considerations for each data type. I’m sure this can be memorised eventually. It would be good to make a post which is a good reference for the syntax for declaring variables of each data type.

Apex variables are Case-Insensitive

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

Apparently, this code will throw an error. I’m pretty sure that’s different to Java, because in Java you can redefine variables as you go can’t you?

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 code block.

Variables declared within a method can only be assessed within the method. However, variables declared within the class can be accessed throughout the class.

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 )

w

Connecting to %s