Public, Private, Protected, And Global Keywords In Apex Programming

Keywords in Apex Programming that specify access levels

Public, private, protected, and global are referred to as access modifiers in Apex Programming, and Apex supports these four modifier types.

1. Accessible to the public

This keyword is utilized to define a class, method, or variable that can be accessed by any Apex code within the same application or namespace.

Example

public class Foo {
    public void quiteVisible();
    private void almostInvisible();
}
2. Restricted access to within the defining class or method.

This keyword is employed to define a class, method, or variable that is limited to local knowledge, within the specific section of code where it is declared. It serves as the default scope for methods and variables lacking explicitly defined scopes.

Example

public class OuterClass {
    private static final Integer MY_INT;
}
3. Accessible within the defining class and its subclasses.

This keyword designates a method or variable that is viewable by any inner classes within the defining Apex class.

Example

public class Foo {
    public void quiteVisible();
    protected void lessVisible();
}
4. Accessible from any Apex code in the organization, regardless of the namespace.

Specifies a class, method, or variable that can be utilized by any Apex code with access to the class, extending beyond the confines of the same application.

Example

global class myClass {

	webService static void

	makeContact(String lastName) {
	// do some work
	}
}

See reference to understand more about apex programming access modifiers.