android - What does new Foo(bar) {public void baz(){....} }; mean in Java? -


i'm trying understand navigationdrawer sample android sdk , met this:

 actionbardrawertoggle mdrawertoggle = new actionbardrawertoggle(             this,                  /* host activity */             mdrawerlayout,         /* drawerlayout object */             r.drawable.ic_drawer,  /* nav drawer image replace 'up' caret */             r.string.drawer_open,  /* "open drawer" description accessibility */             r.string.drawer_close  /* "close drawer" description accessibility */             ) {         public void ondrawerclosed(view view) {             getactionbar().settitle(mtitle);             invalidateoptionsmenu(); // creates call onprepareoptionsmenu()         }          public void ondraweropened(view drawerview) {             getactionbar().settitle(mdrawertitle);             invalidateoptionsmenu(); // creates call onprepareoptionsmenu()         }     }; 

when these methods called, right after instantiation? i'm not familiar syntax. how work? thanks

this anonymous inner class. given interface or class foo 0 or more abstract methods, can use:

foo blech=new foo(){     void bar(int baz){         system.out.println("quux");     } } 

to create instance of foo methods implemented or overridden. abstract methods(of there may 0) need implemented in braces. constructor still called usual , parameters can passed in parentheses.

these used listeners or other objects need run , should specify different actions without creating new classes extending or implementing them.

these compile down [outer class]$[number].class, once each anonymous inner class used in code, if never reached or used multiple times


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -