A list of funny and tricky Apex examples
Inspired by wtfjs
Boolean b;
if(b!=true) system.debug('b is not true');
if(b!=false) system.debug('b is not false');
See Advanced Apex Programming in Salesforce for explanation.
String x = 'Abc';
String y = 'abc';
System.assert(x == y); // passes
System.assertEquals(x, y); // fails
See explanation on StackExchange
Nothing prevents you from recreating a class with the same name of one that exists in the System
(default) namespace.
public class Database {
public static List<sObject> query(String qry){
System.debug(qry);
return null;
}
}
Running Database.query('foo')
will call our new class (essentially overriding the Database methods)!?
The same principle also applies to standard SObjects:
public class Account { }
Account acc = new Account();
acc.AccountNumber = '123'; // Variable does not exist: AccountNumber