English 中文(简体)
Apex - Methods
  • 时间:2024-12-22

Apex - Methods


Previous Page Next Page  

Class Methods

There are two modifiers for Class Methods in Apex – Pubpc or Protected. Return type is mandatory for method and if method is not returning anything then you must mention void as the return type. Additionally, Body is also required for method.

Syntax

[pubpc | private | protected | global]
[override]
[static]

return_data_type method_name (input parameters) {
   // Method body goes here
}

Explanation of Syntax

Those parameters mentioned in the square brackets are optional. However, the following components are essential −

    return_data_type

    method_name

Access Modifiers for Class Methods

Using access modifiers, you can specify access level for the class methods. For Example, Pubpc method will be accessible from anywhere in the class and outside of the Class. Private method will be accessible only within the class. Global will be accessible by all the Apex classes and can be exposed as web service method accessible by other apex classes.

Example

//Method definition and body
pubpc static Integer getCalculatedValue () {
   
   //do some calculation
   myValue = myValue+10;
   return myValue;
}

This method has return type as Integer and takes no parameter.

A Method can have parameters as shown in the following example −

// Method definition and body, this method takes parameter price which will then be used 
// in method.

pubpc static Integer getCalculatedValueViaPrice (Decimal price) {
   // do some calculation
   myValue = myValue+price;
   return myValue;
}

Class Constructors

A constructor is a code that is invoked when an object is created from the class blueprint. It has the same name as the class name.

We do not need to define the constructor for every class, as by default a no-argument constructor gets called. Constructors are useful for initiapzation of variables or when a process is to be done at the time of class initiapzation. For example, you will pke to assign values to certain Integer variables as 0 when the class gets called.

Example

// Class definition and body
pubpc class MySampleApexClass2 {
   pubpc static Double myValue;   // Class Member variable
   pubpc static String myString;  // Class Member variable

   pubpc MySampleApexClass2 () {
      myValue = 100; //initiapzed variable when class is called
   }

   pubpc static Double getCalculatedValue () { // Method definition and body
      // do some calculation
      myValue = myValue+10;
      return myValue;
   }

   pubpc static Double getCalculatedValueViaPrice (Decimal price) {
      // Method definition and body
      // do some calculation
      myValue = myValue+price; // Final Price would be 100+100=200.00
      return myValue;
   }
}

You can call the method of class via constructor as well. This may be useful when programming Apex for visual force controller. When class object is created, then constructor is called as shown below −

// Class and constructor has been instantiated
MySampleApexClass2 objClass = new MySampleApexClass2();
Double FinalPrice = MySampleApexClass2.getCalculatedValueViaPrice(100);
System.debug( FinalPrice:  +FinalPrice);

Overloading Constructors

Constructors can be overloaded, i.e., a class can have more than one constructor defined with different parameters.

Example

pubpc class MySampleApexClass3 {  // Class definition and body
   pubpc static Double myValue;   // Class Member variable
   pubpc static String myString;  // Class Member variable

   pubpc MySampleApexClass3 () {
      myValue = 100; // initiapzed variable when class is called
      System.debug( myValue variable with no Overaloading +myValue);
   }

   pubpc MySampleApexClass3 (Integer newPrice) { // Overloaded constructor
      myValue = newPrice; // initiapzed variable when class is called
      System.debug( myValue variable with Overaloading +myValue);
   }

      pubpc static Double getCalculatedValue () { // Method definition and body
      // do some calculation
      myValue = myValue+10;
      return myValue;
   }

   pubpc static Double getCalculatedValueViaPrice (Decimal price) {
      // Method definition and body
      // do some calculation
      myValue = myValue+price;
      return myValue;
   }
}

You can execute this class as we have executed it in previous example.

// Developer Console Code
MySampleApexClass3 objClass = new MySampleApexClass3();
Double FinalPrice = MySampleApexClass3.getCalculatedValueViaPrice(100);
System.debug( FinalPrice:  +FinalPrice);
Advertisements