function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek KedariAbhishek Kedari 

How to access methods from other classes in apex

Hi,

    How I can access methods from different class ( static / through instance variable ).
    e.g.
           I need something like this - 
public class A {
      public void callA(){
          :
      }
   }

public class B {
      public void callB() {
           A obj = new A();
           obj.callA();
      }
   }

Thanks,
Abhishek
James LoghryJames Loghry
As long as the methods are public or global, the syntax you show in your example will work for instance methods.  For static methods, it would be similar:

public class A {
    public void callA(){}
}

public class B {
    public void callB() {
           A.callA();
    }
}

Boris BachovskiBoris Bachovski
James forgot to add `static` for the `callA` method :)

public static void callA()

More information around static and instance (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm)
James LoghryJames Loghry
Hey man, it's late here ;-)  
Boris BachovskiBoris Bachovski
Excused, though go to bed, there are other people that will take over and help ;)
KeerthigeeKeerthigee
Hi Abhishek,

For static method ina class,

public class sample{
    public static void method(){
       System.debug('Testing');
    }
}
In Annonymous window,  we call as sample.method(); ,then testing output will be diaplayed.

If we access any method except static,then we use

public class sample1{
    public  void method1(){
       System.debug('Access');
    }
}
In Annonymous window, First create object for this class with the help of new operator and then call method with object.

sample1  sam= new sample1();
sam.method1();