5

I would like to use the method from one class within a completely separate class. What code would I use to import the other class?

Class 1

public with sharing class HelperClass {

    public Boolean helperMethod(){

        // Do Stuff

        return true;

    }

}

Class 2

public with sharing class MainClass {

    public Boolean randomMethod(){

        // Somehow pull value from HelperClass.helperMethod()

        return true;

    }

}

This may seem very straight forward to most, but as fundamental functionality in apex, I feel that a basic sample should be provided on this site. When I was learning I had to search through the salesforce forums for much too long to find this answer (granted this was a few years ago).

2 Answers 2

10
public class A {
    Boolean B;
    static Boolean C;
    
    public Boolean getB() {
        return B;
    }
    
    public void setB(Boolean value) {
        b = value;
    }
    
    public static Boolean getC() {
        return C;
    }
    
    public static void setC(Boolean value) {
        C = value;
    }
}

public class D {
    public void E() {
        A.setC(true);
        System.assert(A.getC());
        
        A F = new A(), G = new A();
        F.setB(true);
        G.setB(false);
        
        System.assert(F.getB());
        System.assert(!G.getB());
    }
}

This example illustrates the use of both instance and static members. Here, D can access A through the use of instances F and G. It also has access to A's static methods, getC() and setC(). Use instances when you need to keep track of multiple independent versions of data, and use static members when the data does not need to be tracked independently. This reduces code verbosity, since you no longer have to instantiate classes just to use their methods.

Examples of instance uses:

  • Wrapper classes.
  • Controllers.
  • Data to be serialized in the view state.

Examples of instance methods from the standard library include:

  • List.Sort()
  • String.Substring()
  • EmailAttachment.setBody()

Examples of static uses:

  • Utility methods.
  • Factory methods (factories create instances in a controlled manner).
  • Singleton classes (classes that may only ever have one instance at a time).
  • Functions that operate on their input and do not store the results.

Examples of static methods from the standard library include:

  • String.valueOf()
  • Date.newInstance()
  • EncodingUtil.base64Encode()

Consider how the code is going to be used before you construct it. Use static utility methods whenever possible, and use instance methods only when it makes sense.

1
  • 1
    +1 for explaining static vs. instance and why you'd prefer one over the other. Jul 24, 2013 at 3:50
6

You would go about this by instantiating the first class (HelperClass) within the second class (MainClass) as a variable of type HelperClass. You would then use this newly created variable to reference any methods within the helper class.


HelperClass (Which will be pulled into MainClass)

public with sharing class HelperClass 
{
    public Boolean helperMethod () 
    {
        // Do Stuff
        return true;
    }
}

MainClass

public with sharing class MainClass 
{
    // Instantiate HelperClass as variable "helper" of type HelperClass
    private HelperClass helper = new HelperClass();

    public Boolean randomMethod () 
    {
        // Reference the helperMethod() through newly created helper variable
        Boolean helperMethodValue = helper.helperMethod();
        return true;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .