Provide an example and explanation of anonymous classes in Java




Here we present an example and a short tutorial on anonymous classes in Java. Anonymous classes in Java are more accurately known as anonymous inner classes – there’s no such thing as anonymous classes without the “inner”. That distinction is important, because the fact that they are anonymous inner classes means that they are defined inside another class. If you’ve read our article on inner versus nested classes, then you should be familiar with how inner classes work by now.

An anonymous inner class is an inner class that is declared without using a class name at all – and that of course is why it’s called an anonymous class. An anonymous inner class also has some pretty unusual syntax.

Let’s go through an actual example with some code of an anonymous inner class to help you understand what it is exactly:

Anonymous inner class example:

class ProgrammerInterview  {
 public void read() {
  System.out.println("Programmer Interview!");
 }
}

class Website {
/*  This creates an anonymous inner class: */
ProgrammerInterview pInstance = new    ProgrammerInterview() {
  public void read() {
   System.out.println("anonymous ProgrammerInterview");
  }
 };
}

Understanding our anonymous inner class example

In the code above, you can see that we have two classes – one called Website and another called ProgrammerInterview. The ProgrammerInterview class is pretty straightforward – there’s just a simple method called “read()” that prints the text “Programmer Interview!” when called.

The code that you need to really look closely at is inside the Website class, and is highlighted in the color red. It might look like we are creating an instance of the ProgrammerInterview class called pInstance in that code, but what’s actually happening in that code is that an instance of an anonymous class is being created.

An anonymous inner class is a subclass




Pay special attention to the fact that inside the curly braces – after the “new ProgrammerInterview()” code – there is actually a method definition for a method named “read()”. This certainly does not look like we are creating a normal instance of a class – because you don’t normally see methods being defined at the same time that an instance of a class is created.

What’s actually happening in the code above is that we are creating an instance of a subclass (also known as a child class) of the ProgrammerInterview class. And, the most important thing to understand here is that this instance (pInstance) is actually an instance of an anonymous subclass of the ProgrammerInterview class.

Why is it called an anonymous inner class?

The reason it’s called an anonymous inner class is because the class that we have created clearly has no name! We jump straight to creating an instance of the class, but we do not even give the class a name – all we have is a reference variable (pInstance, in our example above) for the anonymous inner class.

Just to emphasize the syntax differences between creating an anonymous inner class instance and a normal class instance, here is the code for creating a normal class instance – assuming we want to create an instance of the ProgrammerInterview class :

/*Pay attention to the semicolon at the end,
and the use of parentheses instead of braces:
*/
ProgrammerInterview p = new ProgrammerInterview();

The syntax above to create an instance of the ProgrammerInterview class is nothing out of the ordinary, and something you’re probably already familiar with.

Now, let’s look at the code we have for an anonymous inner class:

Anonymous inner class syntax in Java

/*Pay attention to the opening curly braces
  and the fact that there's a semicolon
  at the very end, once the anonymous class is created:
*/

ProgrammerInterview pInstance = new ProgrammerInterview() {
 //code here...
};

Anonymous inner classes and polymorphism




When using anonymous inner classes, polymorphism is actually at work as well. Taking another look at our example above, note that pInstance is actually a superclass reference type that refers to a subclass object. In plain English, that means pInstance is of type ProgrammerInterview (which is the superclass), but pInstance refers to a subclass (or child class) of the ProgrammerInterview class – and this is polymorphism at work. That subclass is the anonymous inner class with no name that is created inside the Website class.

So, what exactly are the implications of an anonymous inner class using polymorphism? Well, it means that using the anonymous inner class reference variable type (pInstance in our example) you can only call methods that are defined inside the type (the class) of the reference variable. Using our example, this means that with pInstance we can only call methods that are defined inside the ProgrammerInterview class.

You might be confused, so let’s take a look at another example to understand exactly what we mean. Suppose we have the following simple classes:

class Animal{
void run() {
}
}

class Dog extends Animal {
void bark() {
}
}

Now, let’s create an instance of the Animal class, but make it so that it points to the class that derives from it, Dog:

class Testing{

public static void main(String[] args)  {

Animal d = new Dog();
/*This is totally legal, calling
  the method run is no problem 
  because it is defined inside 
  the Animal class: */
d.run();

/*Compliler Error! Calling
  the method bark results
  in an error because it is
  not defined in the Animal
  class:  */
d.bark();

}

}

In the code above, the call to “d.run()” is perfectly legal, but the
call to “d.bark()” results in a compiler error because the “bark()” method is not defined inside the Animal class, and our object “d” is of type Animal. To re-emphasize this point, this makes sense because our reference variable type is of type Animal, and even though it refers to a subclass object (from class Dog in this case), it still doesn’t know anything about methods defined in the Dog class.

Now, what does all this have to do with anonymous inner classes? Well, if we try to invoke a method that is defined inside our anonymous class which is not overridden from the superclass, using our anonymous inner class reference, then we will get an error. That sentence must be really confusing, right? But, if you understood the fairly simple example we gave above with the Animal and Dog class, then you shouldn’t have a problem understanding this concept.

And, as always we will give you an example of this scenario. So, take a look:

Example of anonymous class reference accessing non-overridden method

class ProgrammerInterview  {
  public void read() {
 System.out.println("Programmer Interview!");
 }
}

class Website {
ProgrammerInterview pInstance = new ProgrammerInterview() {
 public void read() {
 System.out.println("anonymous ProgrammerInterview");
 }

 public void learn() {
 System.out.println("anonymous, learn ProgrammerInterview");
 }

};

public void readIt()  {
/*
This is legal:
*/
pInstance.read();
/*
Compiler error, the learn method is 
not also defined inside the ProgrammerInterview
class:
*/

pInstance.learn();

}

}

In the code above, we have defined a readIt method that is a part of the Website class. Inside the readIt method, we use the pInstance object that is an instance of the anonymous class that we created earlier. And, we use that pInstance object to call the “learn()” method, as you can see which we highlighted in red.

But, because the learn() method was defined inside the anonymous inner class and not in the ProgrammerInterview class, the pInstance object (which is our anonymous inner class object and is of type ProgrammerInterview) has no idea what the learn() method is. The line “pInstance.learn()” will result in a compiler error – something like “cannot resolve symbol”. Hopefully that all made sense to you – if not, just read it again slowly!

What is the purpose of an anonymous inner class?

You have seen now that by creating an anonymous inner class, we can override one or more methods of a superclass. In our example above, the superclass is the ProgrammerInterview class, and the method being overridden is the read() method.

But, we could have easily done the same thing by just creating a separate class, having it extend the ProgrammerInterview class, and then just override the read() method. So, what is the need to create an anonymous inner class when we could have done the same thing using a normal, separate class?

Well, the main thing is that it is quicker to just create an anonymous inner class rather than create a new separate class. Anonymous inner classes are especially useful when you only need to override a small amount of functionality (like just one method) in a superclass, and don’t want to deal with the overhead of creating an entire class for something so simple.

The anonymous inner class and interfaces

There is actually another way – a second way – to create an anonymous inner class that you should be aware of. It’s basically an anonymous inner class that implements an interface. Read here to find out more about the second way: Anonymous inner class interface.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

  • Grendall R.

    Well explained, good job!

  • My final question. The most important question to me because this is why I started reading about anonymous inner classes in the first place:

    Consider the following code:

    btn.setOnAction(new EventHandler() {
    public void handle(ActionEvent event) {
    System.out.println(“Hello World”);
    }
    });

    This is typical of a JavaFX example.

    I assume what’s happening here is a new ‘anonymous inner class interface’ is being created and passed as an argument to ‘btn.setOnAction’. NOW, the anonymous inner class interface doesn’t have a reference variable assigned to it (like you have done in all your examples). But that’s OK because it is perfectly legal to instantiate without assigning a reference variable, right?

  • Oh, and also:

    3.) What is the point of an anonymous inner class? By using an anonymous inner class we are creating ONE instance of ‘ProgrammerInterview’ that we change one (or more) methods in. This is clearly good because (as you say) we can do this without all the fuss of creating a new class and extending and overriding etc.
    But what about if we planned to create more than one instance where we wanted to change one or more methods? Would we still do a new anonymous class statement for each of these, or would we then say “OK, now I need to extend this class and override this method”? I guess it all depends!!!

  • Excellent article. I now (mostly) understand anonymous classes!

    I have a couple of thoughts:
    1.) You write “pInstance object”. Would it not be more correct to call this ‘pInstance reference variable (of type ProgrammerInterview)’?
    I find this easier to understand, because I see reference variables as ‘remote controls’ that get programmed to control an instance when the ‘=’ sign is invoked. e.g. ‘Animal myDog = new Dog() – myDog is now a remote control for one Dog() instance, but there are no buttons on it for ‘bark()’ because it is of type ‘Animal’.

    2.) I’m slightly confused: The ‘pInstance’ reference variable is assigned to a ProgrammerInterview instance. But clearly this is no ordinary ‘instance’, ‘new ProgrammerInterview();’ would simply create a new instance of the ‘ProgrammerInterview’ class. But instead we add a whole class delcaration, thus making this an anonymous class. But is not strictly anonymous is it? Because it is an instance of ‘ProgrammerInterview’. Its almost like its an ‘objectified-class’ – a class that is trapped inside an instance if you will. Is this what makes it anonymous!?

  • Jason McClinsey

    Wonderfully explained! Thank you!

  • Rafal

    Greate !!! I have never undestrood it before !!!

  • Muhammed Refaat

    Great, and really understandable explanation

  • prashant

    This is really nice! Most online resources are so confusing.

  • varunkumar reddy

    very nice explanation

  • karthik

    i searched explaination for anonymous inner classes in many websites and even The complete reference didn’t helped me ,this explaination is worthfull
    great awesome.

  • Max

    learn() method is used for learning purpose, as name suggests.
    It is up-to u, how to learn..

    Thanks,
    Max

  • Mirsa Mohammed

    Great explanation.

    Thank you..!

  • Andre

    very good job.. i’ve been out of programming(in Java) for a year and this made it just very simple to understand.. thanks!

  • Md Farooq

    very nicely explained

  • Ankur

    I understood all except one thing. What is purpose of learn() method. How can someone access it as it is not accessible with pInstance.

  • Nilesh

    really nice and quickly getting it…thanks …

  • Jawahar Elango

    Thank you. I referred many sites to understand anonymous classes but I got stuck up somewhere and could not proceed further. Here it is very clear and I can say that I know the concept of anonymous classes proudly.

  • sac

    awesome explanation 🙂 🙂

  • tim

    well explained understood the concept very well and also enlightened me about another another one which I had wrongly understood. Thank you so very much for taking the trouble to present things in the most lucid manner

  • abhishek

    Explanation is great but How to create object for the anonymous class ???

  • Saurabh

    nice

  • phpcoder

    Very well explained!!! Thank you!

  • PReeti Wadhwani

    well explained!!!

  • Rajpal Singh

    nicely explained. 🙂

  • soni

    All the topics html,java,c,c++ are brilliantly explained.

  • manu

    awesome article…helped me a lot..thank you

  • neel

    great explanation dude, very nice 🙂

  • messi1987

    good one

  • Shankar

    Very nice:-) loved it

  • krishna

    nice explanation……