Apex Classes

,

Apex, an object-based language, allows you to define classes. Classes contain both states, in the form of variables, and behaviors, in the form of methods.

Voice of the Developer

An Apex class can also define an interface. An interface specifies methods, but does not implement the functionality of the methods with code. Another Apex class can implement the interface, which means that the implementing class provides functionality for all the methods described in the interface.


Defining a Class

To define a Force Platform class, you need to use the following syntax:

access_modifier class class_name {
 //the body of the class
}

The access modifier identifies the visibility of the class. Classes can use the same modifiers as a variable: private, public, and global. A class can be nested within another class. In this scenario, the inner class is, by default, private, meaning that the class is only visible within the outer class.

Caution

Apex classes with methods defined as webservices must use the global access modifier.


The access modifier must be followed by the keyword class, and then the name of the class.

The keyword this refers to the current instance of a class. You use this to reference variables or methods within the class, such as the following:

this.Name = 'Elvis';

Additional keywords can specify that a class implements an already defined interface, or extend an already defined class. A class can implement multiple interfaces, but only extend one class. You use both of these options in code examples in later chapters. For more information on these classes, refer to the Force Platform Apex documentation.

Variables

Apex allows two different types of variables, instance variables and class variables. An instance variable is instantiated when the class is instantiated.

By default, the variables and methods used in a class are instantiated when the class is instantiated. The exception to this default are those variables that are defined with the keyword static.

Static variables and methods are instantiated outside of the instantiation of any individual class, and these variables and methods are available to all instances of the class. Because of this, you can use static variables and methods to share information across multiple versions of a class. For example, you might define a variable as follows:

Public static Boolean firstrun = true;

The first time this class runs, the value of firstrun is true. Code in the class would include an if construct to test to see if the variable is true and take some actions, setting the value to false once the actions are complete. Subsequent instances of this class see the value of firstrun as false.

Class Methods

Apex classes can contain methods. Methods are declared with the following syntax:

Access_modifier data_type name (parameters) {
 code_block;
 }

The optional access_modifier choices for a method are the same as those for a variable. The data_type indicates the data type for the value returned for the method. If a method does not return a value, you can use the keyword void in this position. If you do define a return data type, your code must return a value of that data type in all situations. For instance, if you use an if construct as the final piece in your code, all portions of the construct must return a value of the designated data type.

Method parameters are defined by specifying their data type and then their name. You can have up to 32 input parameters. Although parameters are not required for a method, you always have to include the parentheses.

A constructor is used to create an instance of a class. You do not have to specifically define a constructor for a class—the Force Platform uses the class name, followed by parentheses, as the default constructor. You can define a constructor for the class that accepts arguments.

Properties

A property is similar to a variable, with one significant exception. A property allows you to specify a get accessor, used to retrieve the value of the property, and a set accessor, used to write a value to the property. The syntax for defining a property is as follows:

access_modifier return_type property_name {
 get {
  code_block;
       }
 set {
  code_block;
  }
       }

If you do not specify code for the accessors for a property, the default implementation simply returns the value for the property for the get accessor and writes the value to the property for the set accessor. The following code shows the use of default accessors:

public String company_name {
 get; set;
 }

If you do write a get accessor, the code must return a value with the same data type as the property. You will be using properties and their accessors when you write Visualforce controller extensions and custom controllers in Chapter 12: Extended Visualforce Components and Controllers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.208.203