Posts

Showing posts from August, 2020

Can we define explicit constructor for an anonymous inner class?

Image
The precise answer to this question is " NO ". We can not have an explicit constructor for an anonymous inner class. Generally to create a constructor for a class, the first and the main requirement is "Constructor name must be exactly the same as its class name". But in the anonymous inner class concept, we don't have a class name (at least not till the class file for anonymous inner class gets created.) and that's why we can not define its constructor explicitly as we don't know its class name. So, the bottom line is, explicit constructor concept is not applicable for an anonymous inner class. But the drama doesn't end here. As no class exists in the world of java without a constructor (at least one). The same applies to the anonymous inner class as well. But then immediately question comes to the mind "If we can not write an explicit constructor for an anonymous class then how do we have a constructor in it?" Our superhero "Mr. Comp