Access specifier keywords used in Apex Programming

We can also call public, private, protected & global as access modifiers. Apex allows these 4 types of modifiers.

1. public

This keyword is used to Defines a class or method or variable that can be used by any Apex in this application or namespace.

Example

public class Foo {
    public void quiteVisible();
    private void almostInvisible();
}

2. private

This key word is used to Defines a class/method/variable that is only known locally, within the section of code in which it is defined. This is the default scope for all methods and variables that do not have a scope defined.

Example

public class OuterClass {
    private static final Integer MY_INT;
}

3. protected

This keyword defines a method/variable that is visible to any inner classes in the defining Apex class.

Example

public class Foo {
    public void quiteVisible();
    protected void lessVisible();
}

4.global

Defines 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.

Example

global class myClass {

	webService static void

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

See reference to understand more about apex programming access modifiers.