2

Consider this class:

public class Activity {

    public ArrayList<testInterface> containerListener = new ArrayList<testInterface>();

    public void metodoDiProva(int num) {
        final int finalNum = num;

        containerListener.add(new testInterface() {

            @Override
            public void metodoDiProva() {
                System.out.println(finalNum);
            }
        });
    }
}

Now just look this method:

public void metodoDiProva(int num) {
    final int finalNum = num;

    containerListener.add(new testInterface() {

        @Override
        public void metodoDiProva() {
            System.out.println(finalNum);
        }
    });
}

Imagine the metodoDiProva(int num) is called 2 times, for example:

activityObj.metodoDiProva(10);
activityObj.metodoDiProva(20);

so in the arrayList of the listener there are 2 objects:

The first listener object can use the finalNum variable, the value of this variable is 10.
The second listener object can use the finalNum variable, the value of this variable is 20.

The question is:
In the memory there are 2 different InnerClas or only just 1 ?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Giovanni Far
  • 1,623
  • 6
  • 23
  • 37

1 Answers1

4

No, there is just one inner class; there are two instances of it. When you compile Activity, you will see two .class files: Activity.class and Activity$1.class, where Activity$1.class represents your anonymous inner class.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Ok, thank you, so who keep the value of the finalNum variable where for the firstListener is 10, and for the second is 20? the listenerObject? i mean: are the instances of the anonymous class that keep the value of the final variable? – Giovanni Far Dec 09 '14 at 23:00
  • 1
    The inner class gets an implicit member variable for the local variable behind the scenes. That's also why the local variable must be `final` (or "effectively final" in Java 8), so that the copy's value is guaranteed to be consistent between the inner class and the enclosing class. – rgettman Dec 09 '14 at 23:06
  • this understand, but if u look the code, for the firstListener the value (inside the anonymous class) of the final variable is 10, for the secondListener (inside the anonymous class) is 20. You said to me that there is only 1 class. So... who keep the value 10 for the firstListener and the value 20 for the secondListener? still the inner anonymous class or the instance of the inner anonymous class? – Giovanni Far Dec 09 '14 at 23:12
  • 2
    @GiovanniFar You can use reflection to see fields of your anonymous class. If you use for instance `for (Field f : containerListener.get(0).getClass().getDeclaredFields()){ System.out.println(f); }` you will see something like `private final int name.of.package.YourClass$1.val$num` which means that anonymous class `$1` has field `val$num` which stores value of argument `num`. – Pshemo Dec 09 '14 at 23:14
  • @GiovanniFar Each instance of the anonymous inner class maintains its own value in the implied instance variable. The first instance of the anonymous inner class has the value `10`, and the second instance has the value `20`. – rgettman Dec 09 '14 at 23:14
  • @rgettman So the final variable keeped by the anonymous class are: instance variable (variable of the object of the anonymous class) and NOT static variable of the anonymous class, it is right ? – Giovanni Far Dec 09 '14 at 23:22
  • Correct, those implicit instance variables in the anonymous inner class are not `static`. – rgettman Dec 09 '14 at 23:24