A class is a template or blueprint from which objects are created. An object is an instance of a class.
In Apex, you can define top-level classes (also called outer classes) as well as inner classes, that is, a class defined within another class. You can only have inner classes one level deep. For example:
public class myOuterClass
{
// Additional myOuterClass code here
class myInnerClass
{
// myInnerClass code here
}
}
The following syntax is used for defining classes:
private | public | global
[virtual | abstract | with sharing | without sharing | (none)]
class ClassName [implements InterfaceNameList | (none)] [extends ClassName | (none)]
{
// The body of the class
}
- The
private
access modifier declares that this class is only known locally, that is, only by this section of code. This is the default access for inner classes—that is, if you don't specify an access modifier for an inner class, it is consideredprivate
. This keyword can only be used with inner classes. - The
public
access modifier declares that this class is visible in your application or namespace. - The
global
access modifier declares that this class is known by all Apex code everywhere. All classes that contain methods defined with thewebService
keyword must be declared asglobal
. If a method or inner class is declared asglobal
, the outer, top-level class must also be defined asglobal
. - The
with sharing
andwithout sharing
keywords specify the sharing mode for this class. - The
virtual
definition modifier declares that this class allows extension and overrides. You cannot override a method with theoverride
keyword unless the class has been defined asvirtual
. - The
abstract
definition modifier declares that this class containsabstract
methods, that is, methods that only have their signature declared and no body defined.
Useful Documentation