//I am looking for the preferred way to create objects on demand, during
//the execution of a program.
//Example: I have a program that works with objects known as a Marble. I want to
//create Marble objects with different names that I specify during the execution of the
//program. It seems to me that this would require entering the name of the Marble from the
//keyboard into a String variable, and then using that variable to call the creation
//of a new Marble object. The problem is that the compiler balks about a duplicate
//variable. It seems logical that objects should be able to be created as needed,
//but how to do it?
//
//Below is an example program with a Marble class and a MarbleController class. The MarbleController is the main() entrance to the program.
Thank you for any assistance in understand how this is done in Java.
Here is my example code:
public class Marble { public String size = null; public String color = null; //Default Constructor Marble(){ size = "small"; color = "blue"; } } -------------------MarbleController.java import java.util.Scanner; public class MarbleController { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a name for an Marble and press Enter"); String marbleName = keyboard.nextLine(); Marble marbleName = new Marble(); //The compiler understandably balks about duplicate variable names when trying to use the variable "marbleName" as a placeholder for the entered name of a Marble object. } }