You mean, can you write a class which extends another class, which is unknown until runtime? No, you can't. Apart from not being syntactically possible, classloaders wouldn't be able to load the subclass because they'd first have to load the superclass, which presumably you're proposing to load from within the subclass which isn't yet loaded, because the superclass isn't loaded yet etc and so on
What were you proposing to do with this? There may be alternatives, such as dynamic proxying, providing these classes are all at least related
public class A {}
public class B extends A {}
...
String className = ...read from a config file, perhaps?...
//perhaps className is "B"
A a = Class.forName(className).newInstance();
...use a...
Is that what you meant?
> > public class A {}
> public class B extends A {}
> ...
> String className = ...read from a config file,
> perhaps?...
> //perhaps className is "B"
> A a = Class.forName(className).newInstance();
> ...use a...
>
> Is that what you meant?
I took it to mean something like
public class A extends Class.forName(getClassNameFromSomewhere()) {}
which is, of course, nonsense
> > I bet his actual problem is a candidate for the
> strategy pattern.
>
> You're on. I bet a donut his solution is actually
> going to be via the
> Decorator Pattern. Anyone else what to place a bet?
That's hardly fair! You can just give him a solution, use the decorator pattern, and tax a donut from me!
edit: actually, I want a hedge bet that we never hear about it again
Message was edited by:
georgemc
> I need to create a class, which extends another class
> which i want to be dynamically loaded using
> Class.forname().. Is that possible?
Class.forName will load a class,and return a instance of Class to you
if you really want to dynamic to creat a class,the CGLIB may help you out
google:CGLIB
Message was edited by:
iamyess
> > I need to create a class, which extends another
> class
> > which i want to be dynamically loaded using
> > Class.forname().. Is that possible?
>
>
>
> Class.forName will load a class,and return a instance
> of Class to you
>
> if you really want to dynamic to creat a class,the
> CGLIB may help you out
>
> google:CGLIB
>
> Message was edited by:
> iamyess
What he seems to be after is to extend a class unknown until runtime. CGLIB won't do that, I don't think anything will, and it makes no sense to anyway, really. What use would it be? But I suspect his actual problem will be a lot simpler to solve than all of this