MaDKit forum

Welcome All !
Please, use explicit topic names when creating new threads. Thanks.

You are not logged in.

#1 2011-12-23 14:13:54

SimonHV
Member
Registered: 2011-11-26
Posts: 4

Instancier une classe à partir d'un String

Bonjour,

j'ai le nom d'une classe qui est contenu dans une variable de type String.
Je voulais savoir si il était possible d'instancier la classe contenu dans le String directement à partir de cette variable String.

Par exemple :
String str = new String("Voleur");
str vol = new str();

Je sais que ça ne s'écrit pas comme ça mais je n'arrive pas à trouver de solutions.

Merci beaucoup !

Offline


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/clients/7e4bc2f7e893962ca21d7e305619d422/web/madkit/forum/include/parser.php on line 821

#2 2011-12-27 11:13:25

fmichel
Administrator
From: Montpellier
Registered: 2009-03-24
Posts: 178
Website

Re: Instancier une classe à partir d'un String

Bonjour,

Il faut utiliser :
Class<?> c = Class.forName(str);
Object o = c.newInstance();

le totu dans un try avec les exceptions correspondantes.

Plus d'info dans la javadoc si nécessaire.

Offline

Board footer