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
AlasAlas 

Can't Inner class access the variable of Outer class?

I am getting an error for  the below code

 

Public Class MyOuter

{

     Interger i = 10;  // Variable of Outerclass

   

     Public Class MyInner

      {
       //Here I am accessing outer class variable

       i = 1+10; // Here I am getting an error message as   

      }

}

 

Can't Inner class access the variable of Outer class?

 

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

No, as the inner class doesn't know anything about the instance of the outer class that you'd like to associate it with.  You can access static variables, but not instance variables.

 

If you need this, you need to pass a reference to the MyOuter instance to the constructor of MyInner.