Salesforce Menu

Apex Keywords

1. Class: This keyword is used to define a class.

2. Abstract: This keyword is used to define abstract classes. An abstract class that contains methods only have signature and no body defined. Can also defined methods.

3.Implements: This keyword is used declare a class that impediments an interface.

4.extends: Defines a class that extents another class

5. interface: This keyword is used to define a data type with method signatures. Classes implement interfaces. An interface can extend another interface.

6.virtual: This keyword Defines a class or method that allows extension and overrides. You can’t override a method with the override keyword unless the class or method has been defined as virtual.

With sharing & Without sharing keywords

With sharing Keyword: This keyword enforces sharing rules that apply to the current user. If absent, code is run under default system context.

Example:

public with sharing class MysharingClass {
    // Code will enforce current user’s
    // sharing rules
}

Without sharing keyword: Ensures that the sharing rules of the current user are not enforced.

Example:

public without sharing class noSharing {
    // Code won’t enforce current user’s
    // sharing rules
}

Below are the some important keywords in apex

1. static: This keyword defines a method/variable that is only initialized once, and is associated with an (outer) class, and initialization code. We can call static variables/methods by class name directly. No need of creating instance of a class.

Example:

public class OuterClass {
    // Associated with instance
    public static final Integer MY_INT;
    // Initialization code
         static {
             MY_INT = 10;
         }
}

2. final: This keyword is used to Defines constants and methods that can’t be overridden.

Example:

public class myCls {
    static final Integer INT_CONST;
}

3. this: This keyword represents the current instance of a class , in constructor chaining.

Example:

public class Foo {
    public Foo(String s) { /* … */}
    public foo() {
    this(‘memes repeat’);
    }
}

4. super: This keyword invokes a constructor on a superclass.

Example:

public class AnotherChildClass extends InnerClass {
    AnotherChildClass(String s) {
        super();
        // different constructor, no args

    }
}

5. return: This keyword returns a value from a method.

Example:

public Integer sum() {
    return int_var;
}

6. transient: This keyword declares instance variables that cannot be saved, and should not be transmitted as part of the view state, in Visualforce controllers and extensions.

Example:

transient integer currentValue;

7. null: This keyword defines a null constant that can be assigned to any variable.

Example:

Boolean b = null;

Subscribe Now