2

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!

corgichu
  • 2,580
  • 3
  • 32
  • 46
  • They're called "anonymous inner classes". They were added primarily for the Swing Event model, introduced in Java 2. – paulsm4 Oct 01 '12 at 05:46

3 Answers3

5

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?

Community
  • 1
  • 1
Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
3

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. .

ngesh
  • 13,398
  • 4
  • 44
  • 60
1

It is called an anonymous class.