Salesforce Apex allows you to use the privateprotectedpublic, and global access modifiers when defining methods and variables. In this Salesforce tutorial, we will learn about different Apex Access Modifiers.

Apex Access Modifiers

private

If you declare a class as private it is only known to the block in which it is declared.

  • By default all the inner classes are private.

public

If you declare class as a public, this class is visible throughout your application and you can access the application anywhere.

global

If you declare a class as global, this apex class is visible to all the apex applications in the application or outside the application.

  • If a method or a class(inner) is declared as global, then the top level class also must be declared as global.

With Sharing

If you declare a class as a With Sharing, Sharing rules given to the current user will be taken into the consideration and the user can access or perform the operations based on the permissions given to him on object and fields (Field Level Security, Sharing rules).

Without Sharing

If you declare a class as a Without Sharing, then this Apex class runs in system mode which means Apex code has access to all the objects and field irrespective of current users sharing rules, field level security and Object permissions.

  • If the class is not declared as With Sharing or Without Sharing then the class is by default taken as With Sharing.
  • Both inner classes and outer classes can be declared as With Sharing.
  • If inner class is declared as With Sharing and top level class is declared as Without Sharing, then by default entire context will run in With Sharing Context.
  • If a class is not declared as With/Without Sharing and if this class is called by another class in which sharing rules is enforced then both the classes run with With Sharing.
  • Outer class is declared as With Sharing and inner class is declared as Without Sharing, then inner class runs in Without Sharing Context only(Inner class don’t take the Sharing properties from outer class).

Virtual

If a class is declared with Keyword “Virtual”, then this class can be extended (Inherited) or this class methods can be overridden by using a class called ‘overridden’.

Abstract

This class contains Abstract methods.

Example 1

</>
Copy
public class outterclass {
    //statement(s)
    Class innerclass {
        //statement(s)
    }
}

Example 2

</>
Copy
public With Sharing class Sharingclass {
	//statement(s)
}

Example 3

</>
Copy
public Without Sharing class nonsharing {
	//statement(s)
}

Example 4

</>
Copy
public With Sharing Class Outer {
	//statement(s)
	Without Sharing class inner {
		//statement(s)
	}
}

In the above example, outer class runs with current user Sharing rules. But inner class runs with System Context.

Example 5

</>
Copy
public Without Sharing class outer {
    \\outer class code
	{
		\\inner class code
	}
 }

In the above example, both inner and outer classes runs with current user’s permissions.

Conclusion

In this Apex Tutorial, we learned about Apex Access Modifiers with syntax and examples.