0
for (int i=1; i<n; i ++)
{
    someButton.setOnclickListener(new View.OnClickListener{
        @Override
        public void onClick( View...){
            otherMethod(i);  //Note this is inside actionListener and not just the for loop
        }
    });
}

I thought this should not work because we usually call the methode while the for loop is running, but on click event my be after two hours! in fact, this code works, but where the i is stored temporarily? As a static param?

Please help me understand this.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
TiyebM
  • 2,684
  • 3
  • 40
  • 66
  • Are you sure that this works? I can't compile it because `i` is variable which changes its value. It could compile if `i` would be final or since Java 8 effectively final (if it wouldn't change its value). – Pshemo Apr 21 '19 at 10:10
  • I see, but I just want to clear the idea, the passed value here is made inside the loop and not exactly i and shoul be final. – TiyebM Apr 21 '19 at 10:13

0 Answers0