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
ManojjenaManojjena 

Not able to access global variable in inner class methods

HI All,

 

I have declared a global variable in outer class and trying to access that varibale in inner class method it is showing error.

 

Can any one help me plz it very urgernt.

global class GlobalTest{   

    global String value='Situ';      

    public class Myclass{            

          public void  gtestMethod(){                  

               System.debug(value);     

          }

      }  

}

Error is like Variable does not exist: value at line 6 column 24

Thanks

Situ

jbroquistjbroquist

You can pass an instance of the parent class in to the constructor of the inner class like so...

global class GlobalTest
{   
    global String value='Situ';

    public class Myclass
    {
        private GlobalTest _gb;

        public Myclass(GlobalTest gb)
        {
            _gb = gb;
        }
        
        public void gtestMethod()
        {                  
            System.debug(_gb.value);     
        }
      }  
}

 

KaminiKamini

You can define the variable ('value') as static then this variable can be used in inner class. Please us the following code:

 

global class GlobalTest{
    global static String value='Situ';
    public class Myclass{
        public void gtestMethod(){
        System.debug(value);
        }
    }
}

 

Thanks,