Different types of access modifiers available in Apex
Access Modifiers In Apex

Different types of access modifiers available in Apex

In this blog post,we will take a look at different types of access modifiers which are available in Apex programming.
Apex has 4 different types of access modifiers.

  1.  Public
  2.  Private
  3.  Protected
  4.  Global

Let’s discuss them one by one.

1) Global:

The method or variable with global access modifier can be used by any apex code that has access to the apex class,not just the apex code in the same application. If you want a method which can be referenced outside of the application, either in SOAP API or by other apex code, in that case you will use global access modifier. If you have declared a method or variable with global keyword, you must declare the class with global keyword as well.

2) Public:

if you are using public access modifier with a method or variable, that means that variable or method can by used by
any apex in this application or namespace. Public access modifier in apex is not similar to the one in java. If you want to make something public like java, in that case you have to use global access modifier.

3) Protected:

If you are using protected access modifier with a variable or method then, that method or variable is visible to any inner classes in the defining apex class and also visible to the classes that extend from the defining apex class. You can use this protected modifier with instance methods and member variables.

4) Private:

This is the default access modifier and the method or variable defined with private access modifier is only visible inside the apex class in which they are defined. When you don’t specify any access modifier then private is the default access modifier.

To learn more: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm

Leave a Reply

Close Menu