I want to know why anonymous inner classes in java are marked final. I have read through this post but unable to understand the concept. Even JLS specifies all anonymous inner classes are implicitly final. Can someone elaborate on this concept?
-
2*Well, it would be pretty useless to be able to subclass an anonymous class. The only spot where you would be able to refer to the anonymous class would be in the statement where it's defined (as your hypothetical pseudocode example shows). This means that the program would be guaranteed never to create any instances of the anonymous superclass—and that a smart compiler should be able to collapse the two definitions into one class.* – Luiggi Mendoza Jul 18 '13 at 05:36
-
*More practically, when a class is final, compilers and VMs are free to inline its methods at the calling sites. So in any situation where it is naturally impossible to extend a given class, it makes sense to make such classes intrinsically final.* – Luiggi Mendoza Jul 18 '13 at 05:36
-
1Comments taken from http://stackoverflow.com/a/8584170/1065197 – Luiggi Mendoza Jul 18 '13 at 05:37
-
Hey, yes i did refer to the article in my question. I was not able to grasp the reason properly. – benz Jul 18 '13 at 05:49
4 Answers
Final means they cannot be extended. How would you extend an anonymous inner class? It doesn't even have a name to reference it by. If you need to extend it, you should make it a proper class.
-
*If you need to extend it, you should make it a proper class* check the possible dup to see that *it can be extended*. – Luiggi Mendoza Jul 18 '13 at 05:43
-
When I declare a class with only `private` constructors, it also cannot be extended. But such a class still doesn’t automatically become `final`. As a fun fact, anonymous inner classes actually aren’t final too. [Now even officially](https://stackoverflow.com/a/54019398/2711488)… – Holger Jan 10 '19 at 18:09
As stated in the answer of the question you refer to: There is just no way to declar
class X extends Y
when Y is an anonymous class. It doesn't have a name therefore it can't be referred to like this. The only place where you potentially could overide it is at the site where it is declared, maybe like this (doesn't actually work:
new (new AbstactX(){
public void doSomthing(){..}
})(){
@Override
public void doSomething(){..}
}
But why would you do that? You might as well directly write the final implementation.

- 77,657
- 34
- 181
- 348
It's not quite clear what JLS 15.9.5 means. When I compile this class
public final class Test1 {
public static void main(String[] args) throws Exception {
Runnable r = new Runnable() {
public void run() {
}
};
}
}
I got Test1.class and Test1$1.class. When I decompile them with javap util I got this
public final class test.Test1 {
public test.Test1();
public static void main(java.lang.String[]) throws java.lang.Exception;
}
and this
class test.Test1$1 implements java.lang.Runnable {
test.Test1$1();
public void run();
}
as you can see the anonymous class is in fact not final. You can extend it, try this
Class<?> cls = Class.forName("test.Test1$1");
Object mock = Mockito.mock(cls);
System.out.println(mock.getClass());
System.out.println(mock.getClass().getSuperclass());
output
class test.Test1$1$$EnhancerByMockitoWithCGLIB$$df433a84
class test.Test1$1
this means Mockito created a class that extends our anonymous class

- 133,369
- 30
- 199
- 275
-
Hi thanks for the comment but i am referrring to JLS 15.9.5, annonymous class declarations. – benz Jul 18 '13 at 05:46
-
-
JLS 15.9.5 meant to say they *should* be final, but `javac` developers didn’t care. As a result, [the specification got changed](https://stackoverflow.com/a/54019398/2711488). – Holger Jan 10 '19 at 18:06
Because anonymous inner classes are used only If you want to make only a single object of this class and that's why you don’t even need to give the class a name. So it doesn't make any sense to give extend it any futher.

- 7,636
- 5
- 37
- 49