When learning Object-Oriented Programming (OOP) in Apex, two key concepts that control how your code is structured and accessed are access specifiers and access modifiers. Understanding these concepts helps you organize your code more efficiently and securely, making your development work smoother. Let’s break down these topics in simple terms for students who are new to programming.
What Are Access Specifiers and Access Modifiers?
- Access Specifiers define who can access your class, methods, or variables from different parts of your code.
- Access Modifiers define how the methods or variables can be used or modified once they are accessed.
These concepts help make your code more secure and maintainable by controlling how different parts of your program interact with each other.
Access Specifiers in Apex
Apex provides four main access specifiers to control the visibility of your variables and methods:
- Private:
- Limits access to within the same class only. Neither methods nor variables declared as
private
can be accessed from outside the class.
- Limits access to within the same class only. Neither methods nor variables declared as
public class Car {
private String engineType; // Only accessible within this class
}
- Protected:
- Accessible within the class and any subclasses that extend it (inherit from it). Use it when you want child classes to access parent class variables/methods.
public class Vehicle {
protected String fuelType;
}
public class Car extends Vehicle {
public void setFuelType(String fuel) {
fuelType = fuel; // Can be accessed by child class
}
}
- Public:
- Variables or methods can be accessed from anywhere within your code.
public class Car {
public String modelName; // Accessible from anywhere
}
- Global:
- Used to make variables or methods accessible across namespaces and packages (even external ones).
global class Car {
global String VIN; // Accessible across packages and namespaces
}
Access Modifiers in Apex
Access modifiers define the behavior of variables and methods once they are accessed. Apex offers several access modifiers:
- Static:
Static
variables or methods belong to the class itself rather than to instances (objects) of the class. They are shared among all instances of the class.
public class Car {
public static Integer totalCars = 0; // Shared across all Car objects
}
- Final:
- When a variable or method is declared as
final
, it cannot be changed or overridden by subclasses.
- When a variable or method is declared as
public class Car {
public final String modelName = 'Tesla'; // Cannot be changed once assigned
}
- Transient:
- The
transient
The modifier is used with variables that should not be saved when serializing objects, such as when saving data in Visualforce controllers.
- The
public class Car {
transient String temporaryData; // Will not be stored when saved
}
How Access Specifiers and Modifiers Change Behavior of Variables and Methods
Access specifiers control where a method or variable can be accessed, while access modifiers control how they behave.
- Variables: Specifiers like
private
control visibility, while modifiers likestatic
orfinal
control behavior. - Methods: Specifiers like
public
define whether a method can be called from outside the class, and modifiers likestatic
define whether a method belongs to the class or to an object.
Example:
public class Car {
private String color; // Only accessible inside the class
public static Integer wheels = 4; // Shared across all objects
public Car(String carColor) {
this.color = carColor; // Constructor to initialize color
}
public String getColor() {
return color; // Method to get the car's color
}
public static Integer getWheels() {
return wheels; // Static method to get the number of wheels
}
}
Static vs Instance Methods
Static Methods:
- Belong to the class and not to any specific object.
- You can call a static method using the class name itself without creating an object.
- Can only access static variables.
Instance Methods:
- Belong to an object of the class.
- You must create an object to call instance methods.
- Can access both instance variables and static variables.
Variables Available in Static Methods vs Instance Methods
- In Static Methods: Only static variables are accessible.
Example:
public static Integer getWheels() {
return wheels; // Can access static variable 'wheels'
}
- In Instance Methods: Both instance variables (specific to each object) and static variables (shared across objects) are accessible.
Example
public String getColor() {
return color; // Can access instance variable 'color'
}
Example: Difference Between Static and Instance Methods
Let’s look at a simple example to understand the difference between static and instance methods.
public class Car {
public static Integer wheels = 4; // Shared by all objects
public String color; // Unique for each object
// Static method (class level)
public static Integer getNumberOfWheels() {
return wheels; // Can only access static variable
}
// Instance method (object level)
public String getColor() {
return color; // Can access instance variable
}
}
// Using the class
Car myCar = new Car();
myCar.color = 'Red'; // Assign instance variable
System.debug(myCar.getColor()); // Calls instance method
System.debug(Car.getNumberOfWheels()); // Calls static method
Output:
myCar.getColor()
will output"Red"
, showing the color of that specificmyCar
object.Car.getNumberOfWheels()
will output4
, since all cars share the same number of wheels, which is a class-level property.
Key Takeaways:
- Access Specifiers control who can see and use variables/methods (e.g.,
private
,public
,global
). - Access Modifiers control how the variables/methods behave (e.g.,
static
,final
). - Static Methods belong to the class itself and can only access static variables, while Instance Methods belong to objects and can access both instance and static variables.
By mastering these concepts, you can write better, more secure, and more efficient code in Apex.