Learn APEX with Real Code Examples
Updated Nov 19, 2025
Code Sample Descriptions
1
Apex Counter and Theme Toggle
public class Counter {
public Integer count = 0;
public Boolean isDark = false;
public void updateUI() {
System.debug('Counter: ' + count);
System.debug('Theme: ' + (isDark ? 'Dark' : 'Light'));
}
public void increment() { count++; updateUI(); }
public void decrement() { count--; updateUI(); }
public void reset() { count = 0; updateUI(); }
public void toggleTheme() { isDark = !isDark; updateUI(); }
}
// Simulate actions
Counter c = new Counter();
c.updateUI();
c.increment();
c.increment();
c.toggleTheme();
c.decrement();
c.reset();
Demonstrates a simple counter with theme toggling using Apex classes and console output (system debug).
2
Apex Simple Addition
public class AddProgram {
public Integer a = 5;
public Integer b = 3;
public Integer sum;
public void calculate() {
sum = a + b;
System.debug('Sum: ' + sum);
}
}
AddProgram ap = new AddProgram();
ap.calculate();
Adds two numbers and prints the result using System.debug.
3
Apex Factorial
public class FactorialProgram {
public Integer n = 5;
public Integer fact = 1;
public void calculate() {
for(Integer i = 1; i <= n; i++) {
fact *= i;
}
System.debug('Factorial: ' + fact);
}
}
FactorialProgram fp = new FactorialProgram();
fp.calculate();
Calculates factorial of a number using a loop.
4
Apex Fibonacci Sequence
public class FibonacciProgram {
public void generate() {
List<Integer> fib = new List<Integer>{0,1};
for(Integer i=2;i<10;i++) {
fib.add(fib[i-1]+fib[i-2]);
}
System.debug('Fibonacci: ' + fib);
}
}
FibonacciProgram fp = new FibonacciProgram();
fp.generate();
Generates first 10 Fibonacci numbers using Apex lists.
5
Apex Max of Two Numbers
public class MaxProgram {
public Integer a = 7;
public Integer b = 10;
public void findMax() {
Integer max = (a > b) ? a : b;
System.debug('Max: ' + max);
}
}
MaxProgram mp = new MaxProgram();
mp.findMax();
Finds the maximum of two numbers.
6
Apex Array Sum
public class ArraySum {
public void sumArray() {
List<Integer> nums = new List<Integer>{1,2,3,4,5};
Integer sum = 0;
for(Integer n : nums) { sum += n; }
System.debug('Sum: ' + sum);
}
}
ArraySum as = new ArraySum();
as.sumArray();
Sums elements of an integer list.
7
Apex Even Numbers Filter
public class EvenNumbers {
public void filter() {
List<Integer> nums = new List<Integer>{1,2,3,4,5,6,7,8,9,10};
for(Integer n : nums) {
if(n % 2 == 0) System.debug(n);
}
}
}
EvenNumbers en = new EvenNumbers();
en.filter();
Displays even numbers from a list.
8
Apex String Concatenation
public class ConcatStrings {
public void concat() {
String str1 = 'HELLO';
String str2 = 'WORLD';
String result = str1 + str2;
System.debug('Concatenated: ' + result);
}
}
ConcatStrings cs = new ConcatStrings();
cs.concat();
Concatenates two strings and prints the result.
9
Apex Counter With Loop Simulation
public class LoopCounter {
public void run() {
Integer count = 1;
while(count <= 5) {
System.debug('Counter: ' + count);
count++;
}
}
}
LoopCounter lc = new LoopCounter();
lc.run();
Counts from 1 to 5 using a loop with debug output.
10
Apex Conditional Increment
public class ConditionalIncrement {
public void run() {
Integer count = 3;
if(count < 5) count++;
System.debug('Counter: ' + count);
}
}
ConditionalIncrement ci = new ConditionalIncrement();
ci.run();
Increment counter only if below 5.