2

How do I access instance variables from inside the anonymous class's method ?

class Tester extends JFrame {

   private JButton button;
   private JLabel label;
   //..some more

   public Tester() {
        function(); // CALL FUNCTION
   }

   public void function() {
      Runnable r = new Runnable() {
         @Override
         public void run() {
            // How do I access button and label from here ?
         }
      };
      new Thread(r).start();
   }
}
saplingPro
  • 20,769
  • 53
  • 137
  • 195
  • 3
    You simply access them, and in fact there should be no problem with doing this. What exactly is not working? – Hovercraft Full Of Eels Apr 05 '13 at 01:45
  • possible duplicate of [How can I access private class members of container class within the anonymouse inner class?](http://stackoverflow.com/questions/7574865/how-can-i-access-private-class-members-of-container-class-within-the-anonymouse) –  Apr 05 '13 at 01:51
  • @HovercraftFullOfEels How do you think it will work ? `this.Foo` looks for the object of `superclass` which is not `Tester` but an unnamed class we call anonymous inner class – saplingPro Apr 05 '13 at 01:53
  • @saplingPro: your comment makes no sense. What is `this.Foo`? – Hovercraft Full Of Eels Apr 05 '13 at 01:54

3 Answers3

9

What you are looking for is a fully qualified address since they are not marked final

final Runnable r = new Runnable() {
public void run() {
    Tester.this.button // access what you need
    Tester.this.label  // access what you need
}};

You use the same access pattern for Anonymous Inner Classes when building ActionListeners and other things as well.

This is explained in the specifications as 15.8.4 Qualified this, something the down voter apparently hasn't read. And didn't read the code for comprehension either.

3

How do I access instance variables from inside the anonymous class's method ?

You simply access them if need be:

class Tester extends JFrame {

   private JButton button;
   private JLabel label;
   //..some more

   public Tester() {
        function(); // CALL FUNCTION
   }

   public void function() {
      Runnable r = new Runnable() {
         @Override
         public void run() {
            System.out.println("Button's text is: " + button.getText());
         }
      };
      new Thread(r).start();
   }
}

More important: Why isn't this working for you?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    I was doing `this.label`. The reason! – saplingPro Apr 05 '13 at 03:29
  • 1
    @saplingPro: there's no need to qualify your variable with `this` in this situation unless there is a name clash -- two variables with the same name, one in the inner and one in the outer class. In this case then Jarrod's answer would be correct. But your question never told us that you were using `this` to begin with making it a very confusing and incomplete question. – Hovercraft Full Of Eels Apr 05 '13 at 03:34
3

the following code may explain you the format is IncloseingClassName.this.VariableName;

class Tester extends JFrame {
int abc=44;//class variable with collision in name
int xyz=4 ;//class variable without collision in name
public Tester() {
function(); // CALL FUNCTION  
}
public void function() {
Runnable r = new Runnable() {
int abc=55;//anonymous class variable
     @Override
     public void run() {
      System.out.println(this.abc);  //output is 55        
      System.out.println(abc);//output is 55 
      System.out.println(Tester.this.abc);//output is 44 
      //System.out.println(this.xyz);//this will give error
      System.out.println(Tester.this.xyz);//output is 4 
      System.out.println(xyz);//output is 4 
            //output is 4 if variable is declared in only enclosing method 
            //then there is no need of Tester.this.abcd ie you can directly 
            //use the variable name if there is no duplication 
            //ie two variables with same name hope you understood :)

     }
  };
  new Thread(r).start();
   }  }
Swap
  • 319
  • 2
  • 6