Are anonymous inner classes private by default? Can I make them public?
I need to access methods by reflection.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can access an anonymous inner class’ methods with reflection. See
getDeclaredMethods().Remember to call
setAccessible(true)on theMethodin order to avoid anIllegalAccessException.Also notice that if there’s a
SecurityManagerthis won’t be possible, see What is the security risk of object reflection?Warning: Take into account that anonymous inner classes are kind of disposable class definitions. Once used, you’ll never need them elsewhere again. Just like @PéterTörök said, it’s hard to tell without more context on your problem, but, if you’ve got control over that class, it would probably be better to deanonymize that class (making a private inner class, or even public), and expose that method to the classes that need it.