[SalesForce] Access a boolean variable from the controller by another helper class

I got a boolean variable in my controller class and I want to access that variable to check a condition in another helper class.

Ex: Class MainController {
       public boolean showValue {get; set;} // globally declared
}

I created an instance of the MainController class in my helper class and tried accessing the boolean value but I get an error saying

"Variable does not exist"

, following is what I tried in the helperClass

Ex: Class HelperClassForController {
      public MainController mcontroller {get; set;}

      public static method1(){
       if(mcontroller.showValue){ // I get the error here
         // Some Code
       }
         return value;
      }
    }

How can I access the boolean variable ?

Best Answer

The error is not with the boolean variable of the controller.. its with the instance variable you have created for the controller.

you are trying to access an instance variable from a static method

if you want to keep your method1 as static, then you have to make the controller variable also as static or you have to move the controller variable inside the method.

Class HelperClassForController {
  public static MainController mcontroller {get; set;}

  public static method1(){
   if(HelperClassForController.mcontroller.showValue){ // I get the error here
     // Some Code
   }
     return value;
  }
}

OR

Class HelperClassForController {

  public static method1(){
    MainController mcontroller = new MainController(); 
   if(mcontroller.showValue){ // I get the error here
     // Some Code
   }
     return value;
  }
}