What is Anonymous inner classes in Java?
Anonymous classes are classes which are “anonymous” in nature. Anonymous means they don’t have any name. Anonymous classes and anonymous inner classes are same as the anonymous classes are always “inner” classes.
Anonymous classes can be created in different ways.
Ok, Lets look in detail about these methods.
Anonymous inner class from a class
Let’s create a simple example.
In the above example, the
Person is an abstract class. The class
AnonymousInnerClassExample has a method
printName which accepts an instance of
Person class. So to use this method, we can create an instance of class
Person and pass that object to the method
Person is an abstract class we need to create a subclass for
JavaPerson, implement its methods then create an object of
JavaPerson and pass the instance to the method
printName as shown below.
Now lets check the code, Do we really need an class
JavaPerson? We only need to extend the
Person. Why cant we change the code in a way which serves the same purpose of the above program but without an additional class
JavaPerson ? Yes it is possible. We can completely avoid the class
JavaPerson if we change the code as below.
Now we can see that we passed a new “instance” of class
Person to the method
printName. Wait a minute!. From the beginning of java learning we are studying that we cannot create an instance of abstract class. But then how this is possible here? The answer is we didn’t create an instance of class
Person. We actually created a new “class which extend” the class
Person but without a name. We provided the implementation for the method
getName along with the creation of new instance of the class
Person. So we have effectively created an anonymous class.
In the last example what happens internally is even though we are not creating a class like
JavaPerson that we created previously, java compiler is creating a class with an arbitrary name and extends the class
Person and gives the implementation that we provided. The pseudo code generated by compiler is given below.
Anonymous inner class from an interface
Anonymous inner class from interface is same as that from abstract or concrete class. This can be explained by the following code.
Here instead of creating a class which extends
Person, java compiler creates a class which implements class
Person (of course with the implementation we provided) and passes the instance of that class to method
Anonymous inner class… Why?
Because some times we just need functionality, not a separate entity. In the previous examples we dont need a separate class
JavaPerson just to pass an instance which extends/implements
Person to the method
printName. There is no point in creating additional classes/code for just this piece of functionality if we are able to achieve the same with less number of keystrokes.
Another advantage of anonymous classes is that because of their “inner” nature, they can access members of their enclosing class. See the example below.
Accessing of member variable
myName is possible only because of we created an anonymous inner class of
Person. This would not be possible if we created a new separate class which extends/implements
Anonymous inner classes are widely used for adding event listeners in java swing, javafx etc.. See the code snippet below.
Anonymous inner class… Properties
- As we have already seen, anonymous inner class can access members of enclosing class
An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final. For example the code below will not compile.
To make above code compile either make variable
count“final” or initialize the variable
countwith any value and then do not assign with a new value (which makes that variable effectively final).
An anonymous class shadows member variables from its enclosing scope if the anonymous class has local variables with same name. An example is given below.
An anonymous class can have static members only when they are constant variables. So in effectively you have to provide
finalmodifier if you are declaring a static variable.
You cannot declare static initializers or member interfaces in an anonymous class.
And so we can see that using anonymous inner class can give us several advantages over a standard class which extends/implements a class. If we don’t need a method of a class called from outside, we can go for anonymous inner classes.