Your First Apex Class
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce platform. It uses syntax that looks like Java and acts like database stored procedures.
Class Structure
An Apex class typically follows this structure:
[access_modifier] class ClassName { // Properties // Methods }
Access modifiers in Apex determine the visibility of your classes, methods, and variables:
- public: Accessible by all Apex code in your org
- private: Only accessible within the declaring class
- protected: Accessible by the declaring class and any class that extends it
- global: Accessible by any Apex code in your org, or other orgs through API calls
Properties vs Variables
In Apex, properties store values and can be accessed by other parts of your code. Variables declared inside methods have limited scope.
public class Person { // Property public String name; // Method with a local variable public void greet() { String greeting = 'Hello ' + name + '!'; System.debug(greeting); } }
Constructor Methods
Constructor methods initialize a new instance of a class. They have the same name as the class and don't specify a return type.
public class Person { public String name; // Constructor public Person(String initialName) { this.name = initialName; } }
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck