android - is this also an example of anonymous derived class? (java basics) -
this sample code android developers website:
public void onclick(view v) { new downloadimagetask().execute("http://example.com/image.png"); } private class downloadimagetask extends asynctask<string, void, bitmap> { /** system calls perform work in worker thread , * delivers parameters given asynctask.execute() */ protected bitmap doinbackground(string... urls) { return loadimagefromnetwork(urls[0]); } /** system calls perform work in ui thread , delivers * result doinbackground() */ protected void onpostexecute(bitmap result) { mimageview.setimagebitmap(result); } }
here, in line new downloadimagetask().execute("http://example.com/image.png");
, new downloadimagetask()
create object of downloadimagetask
class, or create annonymous class extends downloadimagetask
?
for comparison:
in code,
public void onclick(view v) { new thread(new runnable() { public void run() { bitmap b = loadimagefromnetwork("http://example.com/image.png"); mimageview.setimagebitmap(b); } }).start(); }
an object of class derived thread class has been made , instantiated, without saving reference (which makes cannot used again in future), instead of object of type thread. case in former code?
does
new downloadimagetask()
create object ofdownloadimagetask
class
yes. it's inner class, implicitly pass current enclosing instance (hidden) constructor, that's that's unusual constructor call.
an anonymous class creation expression uses new something() { ... }
braces.
from section 15.9.1 of jls:
if class instance creation expression ends in class body, class being instantiated anonymous class.
Comments
Post a Comment