Curriculum
Methods and Classes are the foundation of object-oriented programming in Apex. A Class acts as a blueprint that defines properties and behaviors, while Methods are blocks of reusable code that perform specific tasks. Together, they help developers organize code, improve maintainability, promote reusability, and build scalable Salesforce applications.
In Salesforce development, Classes and Methods are used in Apex Triggers, Batch Apex, Queueable Apex, Web Services, Integrations, Lightning Components, and Custom Business Logic. Understanding these concepts is essential before learning Object-Oriented Programming, Exception Handling, and Advanced Apex Development.
Mastering Methods and Classes helps developers write clean, efficient, and reusable Apex code.
A Class is a blueprint or template used to create objects.
A Class contains:
Classes organize related functionality into a single structure.
Classes help developers:
Without classes, large applications become difficult to manage.
Group related functionality.
Use code multiple times.
Simplify updates.
Control access to data.
Support enterprise applications.
These benefits make classes essential in Apex.
Example:
public class StudentManager {
}
This creates a simple Apex class.
Components:
Open:
Developer Console
Click:
File → New → Apex Class
Enter:
StudentManager
Click:
OK
Salesforce automatically creates the class.
public class StudentManager {
public static void displayMessage(){
System.debug(
'Welcome to Salesforce Apex'
);
}
}
This class contains one method.
A Method is a block of code that performs a specific task.
Methods:
Methods are one of the most frequently used programming structures.
Instead of writing the same code repeatedly, developers can create methods and call them whenever needed.
Benefits:
These benefits improve software quality.
accessModifier returnType methodName(){
}
Example:
public static void displayMessage(){
}
Every method contains:
Controls accessibility.
Examples:
Defines returned value.
Examples:
Identifies the method.
Contains executable code.
These components define method behavior.
A Void Method does not return a value.
Example:
public static void greet(){
System.debug('Hello');
}
Output:
Hello
Void methods are used for actions rather than returning results.
Methods execute when called.
Example:
StudentManager.displayMessage();
Output:
Welcome to Salesforce Apex
Method calls trigger execution.
A return value sends information back to the caller.
Example:
public static Integer getTotalStudents(){
return 500;
}
Output:
500
Returned values can be stored and reused.
public class StudentManager {
public static Integer getStudentCount(){
return 500;
}
}
Calling:
Integer total =
StudentManager.getStudentCount();
System.debug(total);
Output:
500
This demonstrates method return values.
Parameters allow methods to accept input values.
Example:
public static void greet(
String name
){
System.debug(
'Hello ' + name
);
}
Parameters make methods flexible.
StudentManager.greet(
'Rahul'
);
Output:
Hello Rahul
Different inputs produce different results.
Methods can accept multiple inputs.
Example:
public static Integer addNumbers(
Integer a,
Integer b
){
return a + b;
}
Calling:
Integer total =
addNumbers(10,20);
Output:
30
Multiple parameters increase flexibility.
Method Overloading allows multiple methods with the same name but different parameters.
Example:
public static void display(){
}
public static void display(
String name
){
}
Salesforce determines which version to execute.
public class Calculator {
public static Integer add(
Integer a,
Integer b
){
return a + b;
}
public static Decimal add(
Decimal a,
Decimal b
){
return a + b;
}
}
This improves code flexibility.
Static methods belong to the class rather than objects.
Example:
public static void showMessage(){
System.debug('Static');
}
Called directly using the class name.
StudentManager.showMessage();
Output:
Static
Static methods are widely used in Apex.
Instance methods require object creation.
Example:
public void displayStudent(){
System.debug('Student');
}
Must be called using an object.
StudentManager student =
new StudentManager();
student.displayStudent();
Output:
Student
Instance methods support object-oriented design.
Classes often contain variables.
Example:
public class Student {
public String studentName;
}
Variables store object data.
public class Student {
public String name;
public Integer age;
}
These variables define student information.
An Object is an instance of a class.
Example:
Student student =
new Student();
Objects allow developers to use class functionality.
Example:
Student student =
new Student();
student.name = 'Rahul';
student.age = 22;
Object properties now contain values.
System.debug(
student.name
);
Output:
Rahul
Objects store and manage data.
A Constructor initializes objects.
Example:
public Student(){
}
Constructors execute automatically during object creation.
public class Student {
public Student(){
System.debug(
'Object Created'
);
}
}
Output:
Object Created
Constructors simplify initialization.
Example:
public class Student {
public String name;
public Student(
String studentName
){
name = studentName;
}
}
This allows object initialization with values.
Student student =
new Student('Rahul');
The object is initialized immediately.
Access Modifiers control visibility.
Accessible everywhere.
Accessible only inside the class.
Accessible in subclasses.
Accessible across packages.
Proper access control improves security.
private static void internalMethod(){
}
Only the same class can access it.
A utility class contains reusable methods.
Example:
public class Utility {
public static Decimal calculateFee(
Integer students,
Decimal fee
){
return students * fee;
}
}
Utility classes improve code reuse.
A software training company manages student enrollments.
public class StudentManager {
public static String getCourseName(){
return 'Salesforce Development';
}
}
Calling:
System.debug(
StudentManager.getCourseName()
);
Output:
Salesforce Development
This demonstrates practical usage of methods and classes.
Improve readability.
Focus on one task.
Promote reusability.
Improve security.
Improve maintainability.
Support team collaboration.
These practices improve code quality.
Developers should avoid these issues.
Understanding Methods and Classes helps professionals:
Classes and Methods are at the heart of Salesforce development.
Classes are blueprints used to organize data and functionality, while Methods are reusable blocks of code that perform specific tasks. Through variables, methods, constructors, objects, access modifiers, and reusable logic, Classes and Methods help developers build scalable, maintainable, and efficient Salesforce applications. They are fundamental building blocks of Apex programming and Salesforce development.
A Class is a blueprint that contains variables, methods, and business logic.
A Method is a reusable block of code that performs a specific task.
A Static Method belongs to the class and can be called without creating an object.
An Object is an instance of a class.
A Constructor is a special method that executes automatically when an object is created.
They help organize code, improve reusability, and support scalable Salesforce development.
Looking to learn more technologies and programming skills?
Send us your inquiry and start your journey towards a successful IT career.
✔ 100% Secure ✔ No Spam ✔ Instant Response
WhatsApp us