Java和Apex在很多方面都是類似的。 Java和Apex中的變量聲明也是相同的。 下面是一些例子來說明如何聲明局部變量。
String productName = 'HCL'; Integer i=0; Set<string> setOfProducts = new Set<string>(); Map<id, string> mapOfProductIdToName = new Map<id, string>();
請注意,所有變量都賦值為null。
您可以在Apex中聲明變量,如String和Integer,如下所示:
String strName = 'My String';//String variable declaration Integer myInteger = 1;//Integer variable declaration Boolean mtBoolean = true;//Boolean variable declaration
這意味著下面的代碼將會拋出一個錯誤,因為變量“m”已經(jīng)被聲明兩次,并且兩者將被視為相同。
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'); }
//Declare variable Products
List<string> Products = new List<string>();
Products.add('HCL');
//You cannot declare this variable in this code clock or sub code block again
//If you do so then it will throw the error as the previous variable in scope
//Below statement will throw error if declared in same code block
List<string> Products = new List<strings>();
更多建議: