Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android inline class

Tags:

java

android

This might be a dumb question, but I've been thinking about it for some time and I have no idea what it's really called.

So for Android, for OnClickListeners, OnTouchListeners, etc., you're allowed to do the following:

    bio.setOnCancelListener(new OnCancelListener() {

        public void onCancel(DialogInterface arg0) {
            // TODO Auto-generated method stub
        }
    });

Pretty much making a new class inline. What is this called in Java or is it an Android specific thing? Basically, what's going on when this happens? Are you pretty much creating an inner class that implements OnCancelListener since On[blah]Listeners are interfaces?

Thanks!

like image 271
corgichu Avatar asked Mar 17 '26 09:03

corgichu


2 Answers

You are correct. You are creating an anonymous inner class which will implement the methods required by OnCancelListener.

See: How are Anonymous (inner) classes used in Java?

like image 178
Morrison Chang Avatar answered Mar 18 '26 22:03

Morrison Chang


They are in Java.. They are called Anonymous classes since they don't have any name.

This is how java tries to combat its inability to support closures,A popular language feature. .

like image 40
ngesh Avatar answered Mar 18 '26 23:03

ngesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!