Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8301211

Anonymous inner class can access its outer class's local variables

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      When creating a functional interface in a java file, for example, Test1.java, and creating an object of the functional interface by implementing the abstract method in the interface inside the public static void main of the Test1 class, the anonymous inner class can access the local variables in the public static void main of the Test1 class, for example, the variables are called: name, age. However, if the variables are declared in the anonymous inner class, name, age, the anonymous inner class will ignore the local variables of outer class, and only use the variables inside the anonymous inner class. Therefore, the variables are separate from outer class and anonymous inner class. The problem is it can access the local variables of outer class since the interface is a separate class, it should not be able to access the variables of outer class even if it is declared in the main java class, Test1.


            adev Anupam Dev
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: