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
K RoyK Roy 

Non static method cannot be referenced from a static context: ERROR

Hi Team,

I am two apex classes.

In Apex CLASS A:

public class A {
public String otID {get;set;}

public A()
{
otID = B.getotMethod();

}

In Apex CLASS B:

I have written a method:

@AuraEnabled
public string getotMethod()
{
//////
return value;
}

I am getting an error in class A while saving. Please help me on this.

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi ,

Can you use the belowin class A code.
 
public class A {
public String otID {get;set;}

public A()
{
   B classb= new b();
    
otID = classb.getotMethod();

}
}

As it is not a static method and class you need to initialise and get the method then.

If this solution helps, please mark it as best answer.

Thanks,
 
mukesh guptamukesh gupta
Hi KRoy,

beacsue these no Static method so you can not access classB method directly. for the solution you need to create Class Object and after that you can access 
 
In Apex CLASS A:

public class A {
public String otID {get;set;}

public A()
{

ClassB objB = new ClassB();
otID  = objB.getotMethod();

}

In Apex CLASS B:

I have written a method:

@AuraEnabled
public string getotMethod()
{
//////
return value;
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh