Thursday, December 29, 2011

Anonymous Classes

(Or more things I should have learned in college)

One of the things that always tripped me up when I was working on open-source projects in High School was seeing what looked to me to be very strange formatting of code. It would be a jumble of parentheses and curly-braces, and it wouldn't make sense to me. Worse yet, I couldn't find a pattern to try to associate this formatting with. So I just skipped it and modified what I knew.

In College, I took a programming language course where we explored the more "exotic" languages. We got to lisp and lambda functions and once I learned about those, I had all the knowledge needed to understand the weird syntax.

It wasn't until I got into android programming that I saw funny looking code again.and then it all made sense. Here's an example:

searchBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


This threw me for a loop -- method calls are usually in the form of
          object.method(arg1, arg2, ...);
Why was there a new in there?
What's with all the curly braces?
Why wasn't the method call finished before all this new junk was put in?

Well, While I was pondering these things, it all just sort of clicked. It was an Anonymous Class.

What are Anonymous Classes?
Anonymous classes are classes that have no explicit name. As a result they are only used once.

Why Would you use one?
Because you're lazy er...because there are cases where you only need a class only to satisfy a parameter of a function. These are most useful for callbacks. Doing GUI programming, or interfacing with some type of input device, you'll see them often.

How to interpret/use one
Well lets start off with the example above. Lets look at it's method signature:
void setOnClickListener(View.OnClickListener l) ;
That's pretty straightforward, it's a function that takes one argument of type OnClickListener.
Lets look at the OnClickListener class. It's an interface. That means that you can't call a instance of that class, it can only be implemented in a class which defines all it's abstract methods. In this case, setOnClickListerner needs an instance of a OnClickListener class so that it knows what to do when clicked. Here's what you can't do:
      OnClickListener bob = new OnClickListener();
That won't compile because it has an  abstract method, onClick(View v), which needs to be defined. So we can write a class that implements the OnClickListener
class anOnClickListernerForTheStartButtonOnTheMainActivity implements View.OnClickListener {
    onClick(View v)
//In another file
anOnClickListernerForTheStartButtonOnTheMainActivity bob = new anOnClickListernerForTheStartButtonOnTheMainActivity();
But that gets cumbersome, so we do as the first example above does and use anonymous classes.

Hopefully this helps someone else understand the beauty of Anonymous Classes


  1. they're VERY used in java, at least in my experience

  2. Haven't touched programming in years but perhaps knocking out little apps for Android will draw be back...

  3. I think this could be very useful, thanks!

  4. Java is really cool. Thanks for you Post, maybe I'll come back to programming :)

  5. This is a bit over my head, but still very intriguing, will be coming back

  6. Interesting post, Java is something I've always dabbled in, hope to see more of this from you.

  7. I have a buddy learning to read this stuff, but he has a number and logical brain... I have tried to learn it... a little. Only the very basic of HTML code makes any sense to me. If you can make it easier for me to understand then I am all ears.... er.. eyes?

  8. I've never done much work on GUI's so I've never encountered code like this before.