How can I do such a thing?
String N = jTextField0.getText();
MyClass (N) = new Myclass();
Is it even possibe?
Or as my question’s explains, how can I just make a method to create a new object of my specified class just with a different name each time I call it.
I really searched everywhere with no luck.
Thanks in Advance
P.S.
I wish you guys can excuse me for not being clear enough, Just to say it as it is, I made a textfield to get the name of someone who wants to make an account, and I made a class named “Customer”. and a button named “Add”. Now I want every time “Add” is clicked, compiler take what is in my textfield and make an object of the class “Customer” named with what it took from the textfield
It was too hard to read it in comments so I updated my question again, so sorry.
I’m stuck so bad. I suppose my problem is that I didn’t “understand” what you did and only tried to copy it. This is what I wrote:
private void AddB0MouseClicked(java.awt.event.MouseEvent evt) {
String name = NameT0.getText();
Customer instance = new Customer(Name);
Customer.customers.add(instance);
and this is my Customer class:
public class Customer{
String name;
public Customer(String name){
this.name = name;
}
public String getName(){
return name;
}
static ArrayList<Customer> customers = new ArrayList<Customer>();
I’m not entirely sure what you mean by…
…but if I’m interpreting you correctly, I believe what you’re trying to do as make
MyClassaccept a constructor parameter. You can do:Then to create a new instance of
MyClass, do:EDIT
Since you’ve added clarifications in the comments since I first answered this question, I thought I would update the answer to portray more of the functionality that you’re looking for.
To keep track of the
MyClassinstances, you can add them to anArrayList.ArrayListobjects can be instantiated as follows:Then for each
MyClassinstance you wish to add, do the following:Note that the
ArrayListshould not be reinstantiated for each instance that you wish to add; you should only instantiate theArrayListonce.