+ Start a Discussion
sai sai 56sai sai 56 

Variable does not exist. variable - kk and op. how to solve?

public class Calculator2 {
    public integer kk = 34;
    public integer op = 23;
    
    public integer result;
    public static void raj(){
        
       result = kk+kj;
        System.debug(result);
    }
Best Answer chosen by sai sai 56
SwethaSwetha (Salesforce Developers) 
HI Sai,
Alter your code as below
public class Calculator2 {
    public static integer kk = 34;
    public static integer op = 23;
    
    public static integer result;
    public static void raj(){
        
       result = kk+op;
        System.debug(result);
    }
}

The issue with your code is that you are trying to access instance variables kk and kj in a static method raj(). Instance variables are associated with an instance of the class and cannot be accessed directly in a static method.

To resolve the "variable does not exist" error, you need to make the instance variables kk and op also static, or pass them as parameters to the static method raj().




If this information helps, please mark the answer as best. Thank you
 

All Answers

SwethaSwetha (Salesforce Developers) 
HI Sai,
Alter your code as below
public class Calculator2 {
    public static integer kk = 34;
    public static integer op = 23;
    
    public static integer result;
    public static void raj(){
        
       result = kk+op;
        System.debug(result);
    }
}

The issue with your code is that you are trying to access instance variables kk and kj in a static method raj(). Instance variables are associated with an instance of the class and cannot be accessed directly in a static method.

To resolve the "variable does not exist" error, you need to make the instance variables kk and op also static, or pass them as parameters to the static method raj().




If this information helps, please mark the answer as best. Thank you
 
This was selected as the best answer
sai sai 56sai sai 56
thanx