Friday 9 May 2014

Creating an Apex Class Using the Developer Console

Creating an Apex Class Using the Developer Console...


1. Click Your Name > Developer Console to open the Developer Console.

2. Click File > New > Apex Class.

3. Enter HelloWorld for the name of the new class and click OK.

4. A new empty HelloWorld class is created. Add a static method to the class by

adding the following text between the

braces:

public static void sayYou() {

System.debug( 'You' );
}
5. Add an instance method by adding the following text just before the final closing brace:
public void sayMe() {
System.debug( 'Me' );
}

6. Click Save.


You’ve created a class called HelloWorld with a static method sayYou() and an
instance method sayMe() . Looking at the definition of the methods,
you’ll see that they call another class, System , invoking the method debug() on that class, which will output strings.

If you invoke the sayYou() method of your class, it invokes the debug() method of the System class, and you see the output.


 
Now How To Calling a Class Method

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.
 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

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 the HelloWorld 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!