java - How to understand new Class.method(){some code} in android -


i have seen piece of code, works dont understand kind of structure. mean, second parameter doing (from new ..)?

i understand this:

new class() = new class.constructormethod(); new class.anymethod(){} //?? how possible add code between braces? kind of class ot patron admit type of code injection?  setnegativebutton(android.r.string.no,new dialoginterface.onclicklistener()         {             @override             public void onclick(dialoginterface dialog, int which) {              }         }); 

i can't understand meant in first 2 lines.

if asking code in line 4, then, seeing instantiation of anonymous class.

the anonymous class expression consists of following:

  • the new operator

  • the name of interface implement or class extend. in example, anonymous class implementing interface helloworld.

  • parentheses contain arguments constructor, normal class instance creation expression. note: when implement interface, there no constructor, use empty pair of parentheses, in example.

  • a body, class declaration body. more specifically, in body, method declarations allowed statements not.

because anonymous class definition expression, must part of statement. in example, anonymous class expression part of statement instantiates frenchgreeting object. (this explains why there semicolon after closing brace.)

onclicklistener not method, interface in itself.

to understand it, code doing similar following

public class test {      public static void main(string[] args) {           class mydialoginterface implements dialoginterface.onclicklistener {              @override              public void onclick(dialoginterface dialog, int which) {                 //do              }         }         setnegativebutton(android.r.string.no,new mydialoginterface());     }  }  class dialoginterface {     interface onclicklistener {         public abstract void onclick(dialoginterface dialog, int which);     }  } 

in code, instead of first declaring new class implements onclicklistener interface , instantiating in following line, in 1 single sentence.

this useful when need ad hoc implementation of class or interface specific section of code. typically see on listeners , callbacks.

the bottom line can write

new myinterface() { //implement abstract methods here } 

instead of

   class myintefaceimplementation implements myinterface {         //implement abstract methods here     }    new myinterfaceimplementation() 

you can see in that, name suggests, anonymous implementation has no named constructor can called elsewhere in code other define in.

you can instantiate anonymously classes derived both classes , interfaces.

you can pass arguments parent class constructor between parens

new constructorwithparams(params) {    //implementation here }; 

i hope helps.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -