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
sai sai 56sai sai 56 

Static method cannot be referenced from a non static context: void Static_vs_NonStatic.staticMethod()

public class IS_stVSnst {
    
     public integer z= 7;
  public integer x= 9;
  public static integer a= 9;
  public static integer b = 7,result; 
  
  public void kyun(){
    result = z+x;
    system.debug(result);
    
   
  }
  
    public static void how(){
     result = a+b;
  system.debug(result);
    }
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sai,

The code does not have any issue. Can you confirm when you are facing the error. Is it calling this method from anonomous window?

Thanks,
 
Vineeth ReddyVineeth Reddy
The error is exactly what it says.

If you are doing something like below, You will see the error come up :
IS_stVSnst test = new IS_stVSnst ();
test.how();

Try doing this instead :
IS_stVSnst.how();

As you have declared the method how() as static, you cannot invoke the method in a non-static context.
You can only invoke in a static context, which is this : IS_stVSnst.how();