7 Replies Latest reply: Jun 18, 2010 8:57 AM by 800387 RSS

    Inner Class Problem

    843789
      hiii
      i want to know something about innerclass.
      if we have inner class in a method the innerclass instanse does not refer to the method variable.
      it is because the local variable is on stack and inner class instance is on heap. And when method complete the stack is
      destroyed but we can refer the inner class somewhere else and store it or clone it in anyway.

      right or not if any correction need plz. do it

      but if we declare the variable final in that case we can refer from innerclasss object
      how and why???

      NOTHING IS STATIC
        • 1. Re: Inner Class Problem
          EJP
          if we have inner class in a method the innerclass instanse does not refer to the method variable.
          Cannot refer to a local variable in any enclosing scope unless it is final.
          it is because the local variable is on stack and inner class instance is on heap.
          Correct.
          And when method complete the stack is
          destroyed but we can refer the inner class somewhere else and store it or clone it in anyway.
          Correct.
          but if we declare the variable final in that case we can refer from innerclasss object
          how and why?
          It is secretly copied. 'final' is there to prevent you changing it in the outer scope, as that won't be reflected in the copy.
          • 2. Re: Inner Class Problem
            843789
            It is secretly copied.  
            where it is copied ?
            in the heap or anywhere else?
             as that won't be reflected in the copy
            what is that means plz. elaborate it


            THANKS
            • 3. Re: Inner Class Problem
              EJP
              where it is copied ?
              Into a secret field in the inner class.
              what is that means plz. elaborate it
              As you observed, the original variable can disappear with its scope, but the innt class can outlive that scope. So the variable is basically copied into a secret variable of the same name in the inner class. If the original variable wasn't final there would be an implication that changing it would also change the copy. It doesn't. So you are prevented from falling into this trap by having to make the variable final.
              • 4. Re: Inner Class Problem
                843789
                Thanks ejp
                • 5. Re: Inner Class Problem
                  843789
                  One more thing

                  can u tell me what benifits(OOPS principle) we get creating an inner class rather then a seperate class
                  which is more easy to understand. why java give concept of inner class.
                  • 6. Re: Inner Class Problem
                    EJP
                    It just saves a lot of boilerplate.
                    • 7. Re: Inner Class Problem
                      800387
                      GouravBansal wrote:
                      One more thing

                      can u tell me what benifits(OOPS principle) we get creating an inner class rather then a seperate class
                      which is more easy to understand. why java give concept of inner class.
                      You get to do a number of nice tricks with encapsulation with inner classes (and inner interfaces). The inner class can access the private variables of its enclosing class, and vice versa. Therefore, you can better encapsulate your objects. For example, if I had no inner classes, I would have to create 'helper' classes in the same package. If I wanted to expose a variable in that helper class, I would create package-private accessors (getters). Then, every class in the package would be able to access that class.

                      With inner classes, I can take this one step further. No class could access, say, a private static inner class. Nor would I have to create accessor methods. I can achieve better encapsulation (at the cost of a larger source file containing more lines of code).

                      - Saish