简体   繁体   中英

Java reflection can't instantiate a private nested class with no arg constructor

I need to create an instance of a private static class which doesn't have a constructor (only the default no-arg constructor). The class is located in a 3d party library.

However, Class.getDeclaredConstructors() returns an empty list for some reason. I created a sample with a private static class within the same codebase, and in this case Class.getDeclaredConstructors() returned a list with 1 constructor (the default one) as expected.

Also calling Class.newInstance() directly results in an exception like

Caused by: java.lang.IllegalAccessException: java.lang.Class<MyClassA> is not accessible from java.lang.Class<MyClassB>

What might be the reason for this strange behavior? Are there any other ways to instantiate a private static class without non-default constructors?

实际上,我正在尝试在 Android API 32 上执行此操作,这可能与这些限制有关。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM