Salesforce Apex Basics Series: Week 1
About This Cheat Sheet
Hey there! Welcome to Salesforce Apex Basics Series: Week 1 — your complete guide to getting started with Apex programming. I've put together everything I learned in Week 1, from Day 1 to Day 5, covering Variables, Classes, Methods, Access Modifiers, Data Types, Operators, Static & Non-Static concepts, and more. Whether you're a beginner starting your Salesforce development journey or brushing up on basics, this cheat sheet covers code examples, memory behavior, and practical tips to help you master Apex fundamentals.
📘 Day 1: Apex Basics
- Variables - Store data values
- Classes - Blueprint for objects
- Methods - Functions inside classes
- Parameters - Inputs to methods
- Return Values - Outputs from methods
- System.debug() - Print/log output
- Flow of Execution - Order of code execution
- Simple Programs - Multiplication, Addition
🔒 Day 2: Access Modifiers
- public - Accessible everywhere
- private - Only within class
- protected - Class + Subclasses
- global - Across orgs/namespaces
- with sharing - Enforce sharing rules
- without sharing - Bypass sharing rules
- When to use each - Best practices
- Example code - Real usage
📊 Day 3: Data Types
- Primitive - Boolean, Integer, String
- Non-Primitive - sObjects, Collections, Objects
- Stack Memory - Primitive values
- Heap Memory - Non-Primitive objects
- Reference Variables - Point to heap
- Pass by Value vs Reference
- Memory Management - Best practices
- Examples - Code demonstrations
⚡ Day 4: Operators
- Arithmetic - +, -, *, /, %
- Comparison - ==, !=, <, >, <=, >=
- Logical - &&, ||, !
- Assignment - =, +=, -=, *=, /=
- Increment/Decrement - ++, --
- Ternary Operator - ? :
- Instanceof - Type checking
- Example Code - All operators
⚙️ Day 5: Static & Non-Static
- Static - Belongs to class
- Non-Static - Belongs to instance
- Memory Behavior - Static vs Instance
- Shared vs Separate Data
- Static Methods - Called on class
- Instance Methods - Called on object
- Static Variables - Shared across instances
- Execution Flow - Real examples
🏁 Key Takeaways
- Apex is Salesforce's language
- Java-like syntax
- Runs on Salesforce platform
- Strongly typed language
- Case-insensitive
- Object-oriented
- Built-in SOQL & SOSL
- Governor limits apply
📘 Day 1
Apex Basics
Variables, Methods🔒 Day 2
Access Modifiers
public, private📊 Day 3
Data Types
Primitive, Non-Primitive⚡ Day 4
Operators
All Operator Types📘 Day 1: Apex Basics
🔤 Variables & Classes
- Variables: Hold data values
- Classes: Blueprint for objects
- Class Structure: public class MyClass { }
- Variable Syntax: Integer num = 5;
- 💡 My Tip: Always use explicit data types
⚙️ Methods & Parameters
- Methods: Functions inside classes
- Parameters: Inputs to methods
- Return Values: Outputs from methods
- Method Syntax: public Integer add(Integer a, Integer b) { return a + b; }
- 💡 My Tip: Keep methods focused on one task
📘 Day 1: Code Example — Multiplication Program
public class Multiplication { public static Integer multiply(Integer a, Integer b) { return a * b; } public static void main() { Integer result = multiply(5, 10); System.debug('Result: ' + result); // Output: 50 } }
🔒 Day 2: Access Modifiers
🔓 public
- Accessible everywhere
- Within class, within org
- Across orgs (for classes/interfaces)
- Use for: Controllers, APIs
🔒 private
- Accessible only within the class
- Default modifier
- Use for: Helper methods, internal logic
- Best practice: Start with private
🛡️ protected
- Accessible within class and subclasses
- Used in inheritance
- Use for: Methods that subclasses need
🔒 Day 2: More Access Modifiers
🌐 global
- Accessible across orgs/namespaces
- For webservices, REST APIs
- Use with @isTest for test classes
- Rarely used in regular Apex
👥 with sharing
- Enforce sharing rules
- Respect user's permissions
- Default for most Apex controllers
- Security best practice
🚫 without sharing
- Bypass sharing rules
- No row-level security
- Use for system-level operations
- Caution: Use only when needed
🔒 Day 2: Code Example — Access Modifiers
public class MyClass { public Integer publicVar = 10; // Accessible everywhere private Integer privateVar = 20; // Only within class protected Integer protectedVar = 30; // Class + Subclasses public Integer getValue() { return privateVar; // private accessed inside class } }
📊 Day 3: Data Types — Primitive vs Non-Primitive
🔹 Primitive Data Types
- Boolean: true / false
- Integer: 32-bit whole numbers
- Long: 64-bit whole numbers
- Double: Decimal numbers
- String: Text values
- Date: Date values
- Time: Time values
- DateTime: Date + Time
🔸 Non-Primitive Data Types
- sObjects: Account, Contact, etc.
- Collections: List, Set, Map
- Custom Objects: MyObject__c
- Arrays: List
- Classes: User-defined types
- Interfaces: Contracts
- Enums: Constants
- Blob: Binary data
🧠 Memory — Stack vs Heap
- Stack Memory: Primitive values stored here
- Heap Memory: Non-Primitive objects stored here
- Reference Variables: Point to heap objects
- Pass by Value: Primitives — copy passed
- Pass by Reference: Objects — reference passed
- 💡 My Tip: Objects are passed by reference, primitives by value
📊 Day 3: Code Example — Data Types
// Primitive Types Boolean isActive = true; Integer count = 10; String name = 'Saurabh Samir'; Double price = 99.99; Date today = Date.today(); // Non-Primitive Types Listnames = new List {'A', 'B', 'C'}; Map scores = new Map {'Saurabh' => 95}; Account acc = new Account(Name = 'Test');
⚡ Day 4: Operators
➕ Arithmetic
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus
🔍 Comparison
- == Equal to
- != Not equal
- < Less than
- > Greater than
- <= Less/Equal
- >= Greater/Equal
🔗 Logical
- && AND
- || OR
- ! NOT
- Example: if (a && b)
📝 Assignment
- = Assign
- += Add/Assign
- -= Sub/Assign
- *= Mul/Assign
- /= Div/Assign
⚡ Day 4: More Operators
🔢 Increment/Decrement
- ++ Increment
- -- Decrement
- Pre: ++i
- Post: i++
❓ Ternary
- Syntax: condition ? true : false
- Example: String result = (age >= 18) ? 'Adult' : 'Minor';
- Short if-else
🔍 instanceof
- Syntax: obj instanceof Type
- Example: if (obj instanceof Account)
- Check object type
⚡ Operator Precedence
- 1: ()
- 2: ++ --
- 3: * / %
- 4: + -
- 5: < > <= >=
- 6: == !=
- 7: &&
- 8: ||
⚡ Day 4: Code Example — All Operators
Integer a = 10, b = 3; // Arithmetic Integer sum = a + b; // 13 Integer mod = a % b; // 1 // Comparison Boolean isEqual = (a == b); // false Boolean isGreater = (a > b); // true // Logical Boolean result = (a > 5 && b < 5); // true // Ternary String status = (a > b) ? 'A is greater' : 'B is greater';
⚙️ Day 5: Static vs Non-Static
📦 Static
- Belongs to the class
- Single copy shared
- Called on class: ClassName.method()
- Loaded when class loads
- Can't access instance variables
📄 Non-Static
- Belongs to each instance
- Separate copy per object
- Called on object: obj.method()
- Loaded when object created
- Can access both static & instance
🧠 Memory Behavior
- Static: One copy in memory
- Instance: One copy per object
- Shared Data: Static variables
- Separate Data: Instance variables
- 💡 My Tip: Use static for utility methods
⚙️ Day 5: Code Example — Static & Non-Static
public class Counter { // Static variable — shared across all instances public static Integer staticCount = 0; // Instance variable — separate per object public Integer instanceCount = 0; public static void incrementStatic() { staticCount++; // All instances share this } public void incrementInstance() { instanceCount++; // Only this object changes } } // Usage Counter c1 = new Counter(); Counter c2 = new Counter(); Counter.incrementStatic(); // staticCount = 1 (shared) c1.incrementInstance(); // c1.instanceCount = 1 (separate) c2.incrementInstance(); // c2.instanceCount = 1 (separate)
🎯 Week 1 Quick Revision
The Golden Rule: Apex is Salesforce's strongly typed, object-oriented programming language. Understanding these fundamentals is essential for building robust Salesforce applications.
Quick Memory Tricks:
- 📌 Day 1: Variables → Classes → Methods → System.debug() — the basic building blocks
- 📌 Day 2: public (everywhere) → private (only class) → with sharing (enforces security)
- 📌 Day 3: Primitives (Stack) vs Objects (Heap) — pass by value vs pass by reference
- 📌 Day 4: Arithmetic (+ - * / %), Comparison (== != < >), Logical (&& || !), Ternary (? :)
- 📌 Day 5: Static = Class level (shared), Non-Static = Instance level (separate)
Key Takeaways:
- Primitives are stored in Stack memory (Boolean, Integer, String, etc.)
- Objects are stored in Heap memory (sObjects, Collections, custom classes)
- Static members are shared across all instances of a class
- Instance members are separate for each object
- with sharing enforces record-level security, without sharing bypasses it
- System.debug() is your best friend for debugging
💡 Pro Tips for Week 1
✅ Practice with System.debug()
Use System.debug() at every step to see what's happening. It's the best way to learn Apex.
✅ Understand Stack vs Heap
Primitives live in Stack (fast), Objects live in Heap (slower). This affects performance and memory.
✅ Use Private by Default
Start with private access modifiers. Only make things public when absolutely needed.
✅ Static Methods First
Start with static methods for utility functions. Use instance methods for stateful operations.
✅ Learn Operators
Master the ternary operator (? :) — it makes your code cleaner and more professional.
✅ Write Small Programs
Write simple programs (addition, multiplication, string concatenation) to practice each concept.
📊 Quick Reference: Data Types
🔹 Primitive Types
- Boolean: true, false
- Integer: 32-bit whole number
- Long: 64-bit whole number
- Double: Decimal number
- String: Text value
- Date: 2024-01-01
- Time: 12:00:00
- DateTime: 2024-01-01 12:00:00
🔸 Non-Primitive Types
- sObjects: Account, Contact, Opportunity
- List: Ordered collection
- Set: Unordered, unique values
- Map: Key-value pairs
- Custom Objects: MyObject__c
- User-defined Classes: new MyClass()
- Enums: Constants
- Blob: Binary data
Download Cheat Sheet
This cheat sheet has 5 pages. Choose download option:
Preview
Still have questions?
We're here to help! Reach out to our support team for any queries.