How do I create an object on the fly
Hello there,
I was wondering if it is possible to create an object on-the-fly. For example:- I have a class called Customer which holds the name, address and phone number of a customer. While the program is running I get a new customer, Mr Brown. How can I create a new Customer object which will hold the details of Mr Brown.
yours in anticipation
seaview
[381 byte] By [
seaviewa] at [2007-10-3 6:56:28]

Don't really understand what you mean creating object on the fly.
But to create an object to store the details of Mr Brown. in your main program.
Customer customer = new Customer();
customer.setName("WHATEVERNAME");
customer.setAddress("WHATEVERADDRESS");
customer.setPhone("....");
Anyway, good luck
On the fly means Is that at run time?
All object are created on-the-fly. Use new MyClassName(argumentsForConstructorIfAny)
On the fly!!!!Sorry if i got my terms mixed up. I meant at runtime.seaview
All objects are created at runtime (except String literals). It's still not clear what your question is.
I'm back agen.
I don't think I explained myself properly. Say I had a store selling bicycles. My computer is on the counter idling away when Mr Brown comes in to buy a bike. I want to enter his details into my program so I type 'Brown' into a text field and when I click enter I want the program to create an object of my Customer class, which will hold Mr Brown's name and address etc.
I don't want to have to stop the program and type Customer brown = new Customer(), but when I type brown into the text field and enter it I want the program to give me a new object named brown.
I tried putting brown into a string like String str = "brown"; then tried Customer str = new Customer() but it thinks I want an object called str.
Any help would be much appreciated.
Regards
seaview.
If I understood you right, you are thinking far too complicated.
So, when you click a button, a new object shall be created and stored. So basically you write a listener to your button that contains a method like this:
public void actionPerformed(ActionEvent e){
Customer newCustomer = new Customer(textfield.getText());
listOfCustomers.add(newCustomer);
}
Maybe what got you confused is the object's name. Remember this: variables and field names DON'T exist anymore at runtime! They are just meant to help you when programming. If you want Mr. Brown as a customer, you have to provide a field in the customer class for the name. If a field is required for the existence of an object, you usually write a custom constructor for it, which accepts an according parameter.
> I'm back agen.
>
> I don't think I explained myself properly. Say
> I had a store selling bicycles. My computer is on
> the counter idling away when Mr Brown comes in to
> buy a bike. I want to enter his details into my
> program so I type 'Brown' into a text field and when
> I click enter I want the program to create an object
> of my Customer class, which will hold Mr Brown's
> name and address etc.
> I don't want to have to stop the program and
> type Customer brown = new Customer(),
Ther's no "stopping the program" here. new Customer(...) is the normal way to create objects. I don't know what other approach you're thinking of, or what benefits you imagine, but most likely you're being driven by false information and/or misunderstanding.