Create a user-generated Java object using Scanner or Joptionpane

Asked

Viewed 14 times

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?

  • 1

    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.

  • And where Hashmap becomes interactive in the creation of objects?

  • 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.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.