Apex methods have a class scope and a visibility scope.
Class scope:
- Instance method — bound to an object instance. Access via
myObj.method(). Can use instance variables. - Static method — class-level. Access via
MyClass.method(). Nothis. Used for utilities and stateless logic.
Visibility scope:
- `private` — visible only within the class (default).
- `protected` — visible to the class and its subclasses.
- `public` — visible to any class in the same namespace.
- `global` — visible to any code, including code in other namespaces (other Salesforce orgs / managed packages). Required for web service methods, batch start/execute/finish, and methods called from outside your namespace.
When to use each:
- `private` + instance — most internal class methods.
- `public` + static — utility methods callable from anywhere in your namespace.
- `global` — only when truly necessary.
globalmethods are part of your public API and changing their signature breaks consumers — they're effectively eternal commitments. Most code shouldn't beglobal.
Common mistake: making everything global "just in case". This locks the API permanently. Use public until you have a concrete reason to expose to other namespaces.
