08-07-2008 06:44 AM
Can load class dynamically like we load in Java using ClassLoader. if there is no direct API then how can we do this in Blackberry. I want to load a class or jar dynamically once i get the object i should call its functions as well.
Prompt response along with code sample would be highly Appriciated !
08-08-2008 07:06 AM - edited 08-08-2008 01:12 PM
I'm not an expert in area, but since no one else has replied, here is my 2p worth.
The short answer is NO.
You can load a new instance of a class dynamically (See Class.forName()), you can then get a new instance of the Class using Class.getInstance(). After that you are a bit stuck. You need to cast the Object you get from the newInstance() to a Class you know about, before you can start calling its methods. But if you knew about it, why didn't you just create an instance of that Class in the first place?
This approach is still very useful when you have various providers of a known Interface. Elsewhere on this forum I think you will see cases of this approach being used to cope with the differences in the OS versions.
08-08-2008 07:39 AM
Thanks for your opinion. like loading a class from Class.forName mean you have all the classes present in you application and you call them. so why use Class.forName we can simply create the instance and then null the object after use.
can i load a class from an external .jar using class.forName ? (as i think , NO) what is you opinion ?
08-08-2008 11:45 AM
You'd want to use Class.forName when you only have the string name of some class that you want an instance of.
As an example, I use it in my persistence code when I unmarshall my data structures from the persistent store. Without going into too much detail, I store classes by their name (and then each class can marshall/unmarshall itself). When I'm pulling classes out I do a forName on the stored value and then do a newInstance on the class (casting to my "IPersistable" interface that the class must implement). After that, the class can take over and load itself up.
Like Peter said, that's as far as you can take class loading. You can't provide your own implementations of ClassLoader.