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
golugolu 

interface call in static methods

How do i call my interface from apex static methods.

I have a interface with below code:
global interface xyz{
    String a();
    String b();
}

Hi want to call a() and b() in another apex class .
public static void createUpdateActivity(List<Communicato_History__c> lstComHistory){
        List<id> activityids = new List<Id>();
        
        if(activityids.size() > 0 && activityids.size() == 1 ){
            // xyz.a();
         
        }

How to do it? It throws error saying non-static methods cannot be referred from static context.
 
Raju yadavRaju yadav
Hi Golu,
You can not call interface method without implementing the interface.
Madhukar_HeptarcMadhukar_Heptarc
Hi Golu,
You Cannot call interface method without implementing.

Can you check reference link :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_interfaces.htm

I hope it will help you.
 
Apex Class:
=========
public class CallInterface implements xyz{
 
    string retrive = 'Community User';
		
    public static void createUpdateActivity(List<Dist__c> lstComHistory){
        List<id> activityids = new List<Id>();
        
        if(activityids.size() > 0 && activityids.size() == 1 ){
            // xyz.a();
        }
}
    public String a()
    {
      return retrive;  
    }
    public String b()
    {
      return retrive;  
    }

}



Thanks & regards
Madhukar_Heptarc
Ajay K DubediAjay K Dubedi
Hi Golu,

You have to just implement your interface by implements keyword.Now if you use interface in your class, it will show error if you not override all the
implemented method.Try the below code.

public class Demo implements xyz{
    public static void createUpdateActivity(List<Communicato_History__c> lstComHistory){
        List<id> activityids = new List<Id>();
        
        if(activityids.size() > 0 && activityids.size() == 1 ){
            // xyz.a();
        }
    }
    public String a() {
        System.debug('In A');
    }
    public String b() {
        System.debug('In B');
    }
}
You can not call override method or any non-static method in static method.
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi