First R0cRid3r
First R0cRid3r

Reputation: 55

Dynamic variable names in Java so each new object has separate name

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>();

Upvotes: 1

Views: 3859

Answers (2)

Jon Newmuis
Jon Newmuis

Reputation: 26540

I'm not entirely sure what you mean by...

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

...but if I'm interpreting you correctly, I believe what you're trying to do as make MyClass accept a constructor parameter. You can do:

public class MyClass {
    private String name;

    public MyClass(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Then to create a new instance of MyClass, do:

String name = jTextField0.getText();
MyClass instance = new MyClass(name);
instance.getName(); // returns the name it was given

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 MyClass instances, you can add them to an ArrayList. ArrayList objects can be instantiated as follows:

ArrayList<MyClass> customers = new ArrayList<MyClass>();

Then for each MyClass instance you wish to add, do the following:

customers.add(instance);

Note that the ArrayList should not be reinstantiated for each instance that you wish to add; you should only instantiate the ArrayList once.

Upvotes: 1

jlordo
jlordo

Reputation: 37843

Variable names must be determined at compile time, they are not even part of the generated code. So there is no way to do that.

If you want to be able to give your objects names, you can use

Map<String, MyClass> map = new HashMap<>();

Add objects to the map like this (e.g):

map.put(userInput, new MyClass());

and retrieve objects like this:

MyClass mc = map.get(userInput);

Upvotes: 2

Related Questions