android - How to pass information to an anonymous class in java ? -


i have following code :

@override public void onreceive(context arg0, intent arg1)  {     string blabla = "blabla";     handler connectionstatushandler = new handler()      {         @override         public void handlemessage(message msg)          {             //do blabla;         }      }; } 

unfortunatly doesn't work because of way java handles closures, can't make blabla final.

so thought maybe can this

@override public void onreceive(context arg0, intent arg1)  {     string blabla = "blabla";     handler connectionstatushandler = new handler()      {         @override         public void handlemessage(message msg)          {             //do localblabla;         }          public string localblabla;     };      connectionstatushandler.localblabla = blabla; } 

but doesn't work because handler doesn't have member called localblabla.

technically can create not anonymous class inherits handler , add there members want, before wonder maybe has trick solve , pass non-final data inner class

thanks

a quick , dirty hack use final string[] 1 value. way value still mutable, can use anywhere need final:

@override public void onreceive(context arg0, intent arg1)  {     final string[] blabla = new string[] { "blabla" };     handler connectionstatushandler = new handler()      {         @override         public void handlemessage(message msg)          {             blabla[0] = "new string value";         }      };     blabla[0] = "i'm still editable"; } 

this thought wanted wrapper class, similar reference, or c++ pointers, lazy write out full interface.

i recommend not using in code intend maintanable. can come cleaner, clearer options if write bit more boilerplate.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -