1 Reply Latest reply: May 1, 2008 9:10 AM by 798906 RSS

    Why method local inner class can use final variable rather than....

    807591
      Hi all

      Just a quick question.

      Why method-local inner class can access final variable defined in method only?

      I know the reason why it can not access instance variable in method.

      Just can not figure out why??

      any reply would be appreciated.

      Steven
        • 1. Re: Why method local inner class can use final variable rather than....
          798906
          Local classes can most definitely reference instance variables. The reason they cannot reference non final local variables is because the local class instance can remain in memory after the method returns. When the method returns the local variables go out of scope, so a copy of them is needed. If the variables weren't final then the copy of the variable in the method could change, while the copy in the local class didn't, so they'd be out of synch.