How do I get this parameter?

38 views Asked by At
public class Test{
   ClassB fieldB;
   public Test(ClassA instanceA){
      fieldB = new ClassB();
      fieldB.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick() {
            ClassC.aStaticMethod(_____);
         }
      });
   }
}

I want to use the instanceA object which is passed as parameter to the constructor of ClassA in the blank. How can I do this? Thanks.

2

There are 2 answers

0
Hovercraft Full Of Eels On BEST ANSWER

Use a final variable/parameter to be able to access it in an anonymous inner class. This is necessary since the anonymous class will make copies of all outer local fields used, and so they must be made final so that their values (usually the reference values) don't change.

public Test(final ClassA instanceA){
0
cmd On

Make the parameter final

public Test(final ClassA instanceA)