c********k 发帖数: 74 | 1 Hi, I got the following exception when running the codes attached below:
Exception in thread "main" java.lang.InstantiationException: IFactory$1
at java.lang.Class.newInstance0(Class.java:281)
at java.lang.Class.newInstance(Class.java:249)
at IFactory.getI(IFactory.java:20)
at IFactory.main(IFactory.java:35)
Is it because Java doesn't support reflection for anonymous inner class? If
so, how can I have dynamic classes? Is it possible?
Thanks in advance.
Gary
import | n*****k 发帖数: 123 | 2
Does this have anything to do the famous reflection error, you must have a
default public constructor, in order to use class.newInstance() method.
【在 c********k 的大作中提到】 : Hi, I got the following exception when running the codes attached below: : Exception in thread "main" java.lang.InstantiationException: IFactory$1 : at java.lang.Class.newInstance0(Class.java:281) : at java.lang.Class.newInstance(Class.java:249) : at IFactory.getI(IFactory.java:20) : at IFactory.main(IFactory.java:35) : Is it because Java doesn't support reflection for anonymous inner class? If : so, how can I have dynamic classes? Is it possible? : Thanks in advance. : Gary
| e***g 发帖数: 158 | 3 it does'nt make sense. anonym. inner class needs access to parent object, even
local variables, how could it be created outside the scope it is defined.
use static public nested class.
【在 c********k 的大作中提到】 : Hi, I got the following exception when running the codes attached below: : Exception in thread "main" java.lang.InstantiationException: IFactory$1 : at java.lang.Class.newInstance0(Class.java:281) : at java.lang.Class.newInstance(Class.java:249) : at IFactory.getI(IFactory.java:20) : at IFactory.main(IFactory.java:35) : Is it because Java doesn't support reflection for anonymous inner class? If : so, how can I have dynamic classes? Is it possible? : Thanks in advance. : Gary
| m******t 发帖数: 2416 | 4
IMO,this problem is caused by that any non-static inner class does
not have a default constructor(i.e. an argumentless one).
They always have a constructor that takes a reference to the
enclosing instance.
There is no way you can do the thing you are trying to
do in the way you are doing it now. 8-) I think you are doing things
in Java with some COM mentality. Dynamic Proxy is probably what
you are looking for...
【在 c********k 的大作中提到】 : Hi, I got the following exception when running the codes attached below: : Exception in thread "main" java.lang.InstantiationException: IFactory$1 : at java.lang.Class.newInstance0(Class.java:281) : at java.lang.Class.newInstance(Class.java:249) : at IFactory.getI(IFactory.java:20) : at IFactory.main(IFactory.java:35) : Is it because Java doesn't support reflection for anonymous inner class? If : so, how can I have dynamic classes? Is it possible? : Thanks in advance. : Gary
|
|