Tuesday 12 March 2013


Important Reserved Words
DescriptionExample
abstractDeclares a class that contains abstract methods that only have their signature and no body defined. Can also define methods.
public abstract class Foo {
  protected void method1() {
    /* - */
  }

  abstract Integer abstractMethod();
}
breakExits the entire loop
while(reader.hasNext()) {
  if (reader.getEventType() == END) {
    break;
  };

  // process
  reader.next();
}
catchIdentifies a block of code that can handle a particular type of exception
try {
  // Your code here
} catch (ListException e) {
  // List Exception handling code here
}
classDefines a class
private class Foo {
  private Integer x;
  public Integer getX() {
    return x;
  }
}
continueSkips to the next iteration of the loop
while (checkBoolean) {
  if (condition) {
    continue;
  }

  // do some work
}
doDefines a do-while loop that executes repeatedly while a Boolean condition remains true
Integer count = 1;

do {
  System.debug(count);
  count++;
} while (count < 11);
elseDefines the else portion of an if-else statement, that executes if the initial evaluation is untrue
Integer x, sign;

if (x==0) {
  sign = 0;
} else {
  sign = 1;
}
enumDefines an enumeration type on a finite set of values
public enum Season {
  WINTER, SPRING, SUMMER, FALL
};

Season e = Season.WINTER;
extendsDefines a class or interface that extends another class or interface
public class MyException extends Exception {}

try {
  Integer i;
  if (i < 5) throw new MyException();
} catch (MyException e) {
  // Your MyException handling code
}
falseIdentifies an untrue value assigned to a Boolean
Boolean isNotTrue = false;
finalDefines constants and methods that cannot be overridden
public class myCls {
  static final Integer INT_CONST;
}
finallyIdentifies a block of code that is guaranteed to execute
try {
  // Your code here
} catch (ListException e) {
  // List Exception handling code
} finally {
  // will execute with or without exception
}
forDefines a loop. The three types of for loops are: iteration using a variable, iteration over a list, and iteration over a query
for (Integer i = 0, j = 0; i < 10; i++) {
  System.debug(i+1);
}

Integer[] myInts = new Integer[] {
  1, 8, 9};
  
for (Integer i : myInts) {
  System.debug(i);
}

String s = 'Acme';

for (Account a :
  [select id, name
   from account
   where name like :(s+'%')]) {
  
  // Your code
}
globalDefines a class, method, or variable that can be used by any Apex that has access to the class, not just the Apex in the same application.
global class myClass {
  webService static void makeContact(
    String lastName) {
  
  // do some work
}
ifDefines a condition, used to determine whether a code block should be executed
Integer i = 1;

if (i > 0) {
  // do something;
}
implementsDeclares a class or interface that implements an interface
global class CreateTaskEmailExample
    implements Messaging.
    InboundEmailHandler {
  
  global Messaging.InboundEmailResult
    handleInboundEmail(
      Messaging.
      inboundEmail email,
      Messaging.InboundEnvelope env){
        
    // do some work, return value;
  }
}
instanceOfVerifies at runtime whether an object is actually an instance of a particular class
if (reports.get(0) instanceof CustomReport) {
  // Can safely cast
  CustomReport c = (CustomReport)reports.get(0);
} else {
  // Do something with the noncustom-report.
}
interfaceDefines a data type with method signatures. Classes implement interfaces. An interface can extend another interface.
public interface PO {
  public void doWork();
}

public class MyPO implements PO {
  public override doWork() {
    // actual implementation
  }
}
newCreates a new object, sObject, or collection instance
Foo f = new Foo();
MyObject__c mo = new MyObject__c(
  name= 'hello');
  
List la = new List();
nullIdentifies a null constant that can be assigned to any variable
Boolean b = null;
overrideDefines a method or property as overriding another defi ned as virtual in a class being extended or implemented
public virtual class V {
  public virtual void foo() {
    /*does nothing*/
  }
}

public class RealV implements V {
  public override void foo() {
    // do something real
  }
}
privateDefines a class, method, or variable that is only known locally, within the section of code in which it is defi ned. This is the default scope for all methods and variables that do not have a scope
public class OuterClass {
  // Only visible to methods and
  // statements within OuterClass
  private static final Integer MY_INT;
}
protectedDefines a method or variable that is visible to any inner classes in the defining Apex class
public class Foo {
  public void quiteVisible();
  protected void lessVisible();
}
publicDefines a method or variable that can be used by any Apex in this application or namespace
public class Foo {
  public void quiteVisible();
  private void almostInvisible();
}
returnReturns a value from a method
public Integer meaningOfLife() {
  return 42;
}
staticDefines a method or variable that is only initialized once, and is associated with an (outer) class, and initialization code
public class OuterClass {
  // associated with instance
  public static final Integer MY_INT;

  // initialization code
  static {
    MY_INT = 10;
  }
}
superInvokes a constructor on a superclass
public class AnotherChildClass extends InnerClass {
  AnotherChildClass(String s) {
    super();
    // different constructor, no args
  }
}
testmethodDefines a method as a unit test
static testmethod testFoo() {
  // some test logic
}
thisRepresents the current instance of a class, or in constructor chaining
public class Foo {
  public Foo(String s) {
    /* - */
  }

  public foo() {
    this('memes repeat');
  }
}
throwThrows an exception, signaling that an error has occurred
public class MyException extends Exception {}

try {
  Integer i;
  if (i < 5) throw new MyException();
} catch (MyException e) {
  // Your MyException handling code here
}
transientDeclares instance variables that cannot be saved, and should not be transmitted as part of the view state, in Visualforce controllers and extensions
transient integer currentValue;
triggerDefines a trigger on an sObject
trigger myAccountTrigger on Account
  (before insert, before update) {

if (Trigger.isBefore) {
  for (Account a : Trigger.old) {
    if (a.name != 'okToDelete') {
      a.addError('You can\'t delete this
      record!');
    }
  }
}
trueIdentifies a true value assigned to a Boolean
Boolean mustIterate = true;
tryIdentifies a block of code in which an exception can occur
try {
  // Your code here
} catch (ListException e) {
  // List Exception handling code here
}
virtualDefines a class or method that allows extension and overrides. You cannot override a method with the override keyword unless the class or method has been defined as virtual.
public virtual class MyException
    extends Exception {

  // Exception class member variable
  public Double d;

  // Exception class constructor
  MyException(Double d) {
    this.d = d;
  }

  // Exception class method
  protected void doIt() {}
}
webServiceDefines a static method that can be used to access external servers. Web service methods can only be defined in a global class.
global class MyWebService {
  webService static Id
  makeContact(String lastName, Account a) {
    Contact c = new Contact(
      lastName = 'Weissman',
      AccountId = a.Id);
      
    insert c; return c.id;
  }
}
whileExecutes a block of code repeatedly as long as a particular Boolean condition remains true
Integer count=1;

while (count < 11) {
  System.debug(count);
  count++;
}
with sharingEnforces sharing rules that apply to current user. If absent, code is run under default system context.
public with sharing class sharingClass {
  // Code will enforce current
  // user's sharing rules
}
without sharingEnsures that the sharing rules of the current user are not enforced
public without sharing class noSharing {
  // Code won't enforce current
  // user's sharing rules
}
Categories: ,

0 comments:

Post a Comment

    Links