0
Usually when creating an object we type Class obj1 = new Class(); But what if I want the user to create interactively on the console and give a name (like this obj1) to the object, as it is done?
0
Usually when creating an object we type Class obj1 = new Class(); But what if I want the user to create interactively on the console and give a name (like this obj1) to the object, as it is done?
Browser other questions tagged java
You are not signed in. Login or sign up in order to post.
This type of metaprogramming is not possible in Java, but there should probably be some other approach to doing what you’re trying to do. You could for example create a
HashMap<String, Classe>
and use what the user typed as the index of its object.– Andre
And where Hashmap becomes interactive in the creation of objects?
– Jairo Araujo
Hashmap allows the user to define the name of the "variable". If the interactivity you want is to allow the user to enter the full line of code, then there is no way to do it in Java anyway, it would need some high level language that supports
eval
.– Andre