Wednesday, 29 August 2012

Calling a Class Method - Apex


Now that you’ve created the HelloWorld class, follow these steps to call its methods.
  1. Execute the following code in the Developer Console to call the HelloWorld class's static method. (See Tutorial 2 if you've forgotten how to do this). You might have to delete any existing code in the entry panel. Notice that to call a static method, you don’t have to create an instance of the class.
    HelloWorld.sayYou();
  2. Open the resulting log.
  3. Set the filters to show USER_DEBUG events. (Also covered in Tutorial 2). “You” appears in the log:
    Hello World class execution in the log
  4. Now execute the following code to call the HelloWorld class's instance method. Notice that to call an instance method, you first have to create an instance of theHelloWorld class.
    HelloWorld hw = new HelloWorld();
    hw.sayMe();
  5. Open the resulting log and set the filters.
  6. “Me” appears in the Details column. This code creates an instance of the HelloWorld class, and assigns it to a variable called hw. It then calls the sayMe() method on that instance.
  7. Clear the filters on both logs, and compare the two execution logs. The most obvious differences are related to creating the HelloWorld instance and assigning it to the variable hw. Do you see any other differences?
Congratulations—you have now successfully created and executed new code on the Force.com platform!

No comments:

Post a Comment