Create Objects on Demand?
//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:
Code Java:
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.
}
}
Re: Create Objects on Demand?
You have to define the names of all variables at compile time.
Quote:
objects should be able to be created as needed,
//but how to do it?
You create new objects by using "new"
Can you describe what you want the program to do and not what technique you want to use?
There are probably ways to do the job besides the one you're trying to use.
For example if you use a Map you can give a "name" to any object that is stored in the map.
Re: Create Objects on Demand?
Like Norm said, describe the program needs to do rather than the technique you want to use.
A possible solution would be you have a loop and in the loop you create new objects and put it in a container like a array, list, map or even a custom container.
Here is some Pseudo:
Code :
int totalMarbles = 20; //amount of marbles > get from console?
Hashtable bag = new Hashtable(totalMarbles);
for(int i = 0; i < totalMarbles; i++)
{
bag.put(i, new Marble(Color.green)); //put 20 marbles in my bag
}
Re: Create Objects on Demand?
You have to define the names of all variables at compile time.
Not quite true, but if a variable has no pointer or variable-name assigned to it, they will get garbage-collected. Assign pointers to the variables with a hashtable, and then set the keynames to the input from keyboard.
Re: Create Objects on Demand?
Do we have the same definition of a "variable"? A source symbol that refers to a memory location.
I don't know of any way to refer to a location in memory without a variable.
Re: Create Objects on Demand?
Not saying thats not the case, but the thing he seems to be intrested in is deciding what the source symbol is supposed to be, which is impossible. However you can use another variable to set key names.
Code :
Hashtable marbleBag = new Hashtable();
marbleBag.put([I][B]theName[/B][/I], new Marble());
this works, but defining mable *variable name* = new marble(); doesnt.
And getting objects is easily done by
Code :
(marble)marbleBag.get([I][B]theName[/B][/I]);