Domanda

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.

È stato utile?

Soluzione 2

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){

Altri suggerimenti

Make the parameter final

public Test(final ClassA instanceA)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top