Retreving the Class Objects at runtime using reflection
I'm using reflection to get the classes at runtime. Can any one suggest how to get the objects associated with that particular class.
For example, i have a class S1 and the objects associated with this class are X1 and X2.
I'm able to get the class name (i.e S1) using reflection.
Is there any method that will return me the objects (X1, X2) associated with this class.
Thanks
Murali
You can either use like
Class theClass = YouClass.clss
then object x1 can be created like
x1 = Class.forName(theClass .getNameI())
you can similarly create tthe object also you can use
x1 = theClass.newInstance()
provided the defualt constructor should not expect any thing as argument for
intialising the class
I have class S1 with some setter and getter methods.
I have created two objects X1 and X2 and using these objects i have called the setter methods.
Now during runtime i get the class name (S1) using reflection, and now i have to call the getter methods using the same objects (X1, X2).
Is there any way to retreive these objects (X1 and X2) of this class so that getter methods in that class can be accessed.
Thanks
Murali
If I understand right, you want to be able to get every instance of a class that has been created in your virtual machine. Can't do that with reflection. You could create a mechanism yourself with, say, a static collection and a constructor that adds the new instance to the collection as soon as it is created.
> If I understand right, you want to be able to get
> every instance of a class that has been created in
> your virtual machine. Can't do that with reflection.
> You could create a mechanism yourself with, say, a
> static collection and a constructor that adds the
> new instance to the collection as soon as it is
> created.
Probably look into JVMPI for that.