1

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>();
6
  • 1
    Containers? HashMap perhaps? Commented Nov 24, 2012 at 22:27
  • 3
    It's not really clear what you're asking, there are already 3 responses answering very different questions. Commented Nov 24, 2012 at 22:30
  • 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. Commented Nov 24, 2012 at 22:40
  • Then Jonathan Newmuis's answer is the one for you. Commented Nov 24, 2012 at 22:43
  • @jlordo How so? Each Add should create a new customer and allow it to be listed, deleted, etc, no? Thus to say that Jonathan's answer is correct leaves out a very important aspect. Commented Nov 24, 2012 at 22:46

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I suspect the "ideal approach" would be to a combination of a List collection and state in the MyClass (e.g. "Customer Name") ..
1

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.

6 Comments

Thanks a lot for your response, I did what you said, and now I'm wondering if I have to make an array to keep the objects it makes? I mean I wrote that and I was expecting to see new objects in my code but it didn't happen so I thought maybe I'm too naive to think that way.
You can keep track of them in an array, but it would likely be better to use a List. Since you have provided quite a bit more information since I first answered your question, I have edited my answer to account for this. Please see the answer above to see how to use an ArrayList to manage your MyClass references.
Since the size of your array would be dynamic, it's best to use a List instance (as suggested). Using an array in that case would be quite cumbersome. Don't forget to accept an answer as well, by clicking the little checkmark near it.
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>();
Thank you very much for the help :) Today I went to our university and asked our programming professor about the solution you gave me and he totally explained them to me. My main problem was that I didn't have a good understanding of arrays and array lists. It worked totally great. Thanks again
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.