Say you're building the Salesforce system for a school that signs students up for classes. Assuming we have the following class defined:
public class Student { public String name; public String hobby; }
You're given a requirement that says: If the student's hobby is football, then sign them up for football.
. We can get 90% of the way there with this code (which you should already be comfortable with):
public class StudentScheduleController { public void signupBasedOnHobby(Student s){ footballSignup(s); // Only call this line if the student's hobby is football } }
But we're missing a conditional check for the Student's hobby. The code above will schedule every student for football. That's not what we want. Let's add an if statement
to check if the student's hobby is football. If it is, we'll run the code that signs them up:
public class StudentScheduleController { public void signupBasedOnHobby(Student s){ if(s.hobby == 'football') { footballSignup(s); } } }
Viola! As easy as that. Now, the footballSignup(s)
method is only called if the student's hobby is football.