If you’re new to programming, starting with Object-Oriented Programming (OOP) in Apex is a great choice! Apex, the programming language of Salesforce, uses OOP principles, which helps make your code modular, reusable, and easier to manage. In this article, we’ll cover the basics of OOP in Apex and break down key concepts like classes, objects, constructors, instance variables, methods, and more. By the end, you’ll have a clear understanding of these concepts and be ready to apply them in your coding journey.
What is Object-Oriented Programming (OOP)?
OOP is a way of programming where everything revolves around objects—real-world entities like cars, people, or even a shopping cart. Objects are created using classes, which are like blueprints for these objects.
Imagine you’re building different cars. The class is the blueprint that defines what properties a car should have (e.g., color, speed), and an object is the actual car you build using that blueprint, like a red car or a blue car.
Understanding Classes and Objects in Apex
- Class: A class in Apex is a blueprint that defines the properties (data) and behaviors (actions) of an object.
- Object: An object is a specific instance of a class. It has actual values for the properties defined by the class.
Example:
public class Car {
public String color;
public Integer speed;
}
Here, Car
is a class that defines two properties (instance variables) color
and speed
.
Creating an Object in Apex:
Car myCar = new Car();
myCar.color = 'Red';
myCar.speed = 100;
In this example, myCar
is an object of the Car
class with the color set to 'Red'
and the speed set to 100
.
What is a Constructor?
A constructor is a special type of method used to create and initialize objects. When you create a new object, the constructor sets up the initial values for the object’s properties.
Example:
public class Car {
public String color;
public Integer speed;
// Constructor
public Car(String carColor, Integer carSpeed) {
color = carColor;
speed = carSpeed;
}
}
The constructor Car(String carColor, Integer carSpeed)
is used to initialize the color
and speed
when the car is created.
Using ‘this’ Keyword in Constructor
The this
keyword refers to the current object. It’s useful when you have a variable inside the constructor with the same name as an instance variable.
public class Car {
public String color;
public Integer speed;
// Constructor using 'this'
public Car(String color, Integer speed) {
this.color = color; // 'this.color' refers to the instance variable
this.speed = speed;
}
}
In the example above, the this
keyword differentiates between the instance variables color
and speed
, and the local variables passed to the constructor.
Instance Variables vs Local Variables
- Instance Variables: These are variables declared inside a class but outside of methods. Each object has its own copy of instance variables. In our
Car
class,color
andspeed
are instance variables. - Local Variables: These are variables declared inside methods and exist only during the method’s execution.
Example:
public class Car {
public String color; // Instance variable
public Integer speed; // Instance variable
public void drive(Integer distance) {
Integer time = distance / speed; // 'time' is a local variable
System.debug('Time to cover ' + distance + ' km is ' + time + ' hours');
}
}
Here, color
and speed
are instance variables, while time
is a local variable that only exists within the drive
method.
Methods: What They Are and How They Work
A method is a block of code that performs a specific task. Methods can work with both instance variables and local variables.
Formal Arguments vs Actual Arguments
- Formal Arguments: These are the parameters defined in the method signature (inside the parentheses).
- Actual Arguments: These are the values passed to the method when you call it.
public class Car {
public void drive(Integer distance) { // 'distance' is a formal argument
System.debug('Driving for ' + distance + ' km');
}
}
Car myCar = new Car();
myCar.drive(100); // '100' is the actual argument passed
Pass by Value vs Pass by Reference
In Apex, primitive types like Integer
, Boolean
, and String
are passed by value, meaning that changes made to the variable inside a method do not affect the original variable outside the method. On the other hand, non-primitive types like objects are passed by reference, meaning that changes made to the object inside the method affect the original object.
Example:
public class Car {
public Integer speed;
public void increaseSpeed(Integer newSpeed) {
newSpeed = newSpeed + 10; // This change will not affect the original variable
}
public void changeCarSpeed(Car car) {
car.speed = car.speed + 10; // This change will affect the original object
}
}
Car myCar = new Car();
myCar.speed = 100;
Integer speedValue = 100;
myCar.increaseSpeed(speedValue);
System.debug(speedValue); // Output will still be 100 (pass by value)
myCar.changeCarSpeed(myCar);
System.debug(myCar.speed); // Output will be 110 (pass by reference)
In the above code:
- When we pass
speedValue
, its value remains unchanged because primitive types are passed by value. - When we pass
myCar
(an object), the speed is updated because objects are passed by reference.
Putting It All Together
Here’s a full example of a simple class in Apex using all the concepts we’ve discussed:
public class Car {
public String color;
public Integer speed;
// Constructor using 'this'
public Car(String color, Integer speed) {
this.color = color;
this.speed = speed;
}
// Method to drive the car
public void drive(Integer distance) {
Integer time = distance / speed; // Local variable
System.debug('Driving for ' + distance + ' km will take ' + time + ' hours');
}
// Method to increase speed (demonstrating pass by value)
public void increaseSpeed(Integer increment) {
speed = speed + increment;
}
// Method to display car details
public void displayDetails() {
System.debug('Car color: ' + color + ', Speed: ' + speed + ' km/h');
}
}
// Creating an object of the Car class
Car myCar = new Car('Blue', 100);
// Calling methods
myCar.drive(200);
myCar.increaseSpeed(20);
myCar.displayDetails();
Output:
Driving for 200 km will take 2 hours
Car color: Blue, Speed: 120 km/h
Conclusion
Object-Oriented Programming in Apex is a powerful way to structure your code. Understanding the basics like classes, objects, constructors, instance variables, and methods will set you on the right path. Remember, practice is key, so try writing small classes and methods on your own!
By learning these concepts, you’re on your way to becoming a confident Salesforce developer!