Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
sarfrzmalik
Posts: 5
Registered: ‎08-07-2008
My Device: Not Specified

Dynamic Class Loading in Blackberry Java

Hi,

 

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 !

 

Thanks

Sarfraz Malik

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Dynamic Class Loading in Blackberry Java

[ Edited ]

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.

Message Edited by peter_strange on 08-08-2008 06:12 PM
New Developer
sarfrzmalik
Posts: 5
Registered: ‎08-07-2008
My Device: Not Specified

Re: Dynamic Class Loading in Blackberry Java

Hi,

 

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 ?

Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: Dynamic Class Loading in Blackberry Java

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.