How to scan a string and use that as new object name? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

How to scan a string and use that as new object name?

For example: Scanner scan = new Scanner(system.in) String str = sc.next(); Now i would like to make an object name based on the input... A str = new A(); How? To make an object name based on str input?

5th May 2019, 4:16 PM
REXH
REXH - avatar
4 Respuestas
+ 4
This is an interesting question. But in Java it does not work. If I'm right, the name of a variable is not a datatype. https://stackoverflow.com/questions/26541743/create-variables-dynamically-data-types-of-java-variables/26541997#26541997 The only thing I can imagine would be to use an other language which creates java code.
5th May 2019, 4:58 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Alright thx guys i will try it
5th May 2019, 5:35 PM
REXH
REXH - avatar
+ 1
You can create an String object: String s = new String(str);
5th May 2019, 5:22 PM
JavaBobbo
JavaBobbo - avatar
+ 1
use Map<String, Object> or HashMap<String, Object> instead
5th May 2019, 5:25 PM
zemiak