Scope
In Apex, scope determines the visibility and lifetime of a variable. Variables can have different scopes, such as local (within a method) or member (within a class).
Example
public class ScopeExample {
// Member variable - accessible anywhere in this class
private String memberVariable = "I am a member variable.";
public void myMethod() {
// Local variable - only accessible within this method
String localVariable = "I am a local variable.";
System.debug(memberVariable); // This is valid
System.debug(localVariable); // This is also valid
}
public void anotherMethod() {
System.debug(memberVariable); // This is valid
// System.debug(localVariable); // This would cause a compile-time error
}
}