10 Replies Latest reply: Mar 14, 2013 7:25 AM by doremifasollatido RSS

    Ahh i'm stuck on this

    996722
      Just a quick question.

      Is it possible to create an object using the name of a previously defined variable?

      For example.

      (lets assume theres already a class created called "Person" with a contructor method for name, as well as getters and setters already made)

      System.out.println("Enter persons name");
      string name = scan.nextLine();
      /* now i want to make an object for that person using the name that was just entered */
      Person name = new Person(name);

      The reason i ask is that I want the program to keep making these objects until the loop is broken, so I don't want to set a limited amount of object names by default.

      Thanks in advance!
        • 1. Re: Ahh i'm stuck on this
          rp0428
          >
          Is it possible to create an object using the name of a previously defined variable?

          For example.

          (lets assume theres already a class created called "Person" with a contructor method for name, as well as getters and setters already made)

          System.out.println("Enter persons name");
          string name = scan.nextLine();
          /* now i want to make an object for that person using the name that was just entered */
          Person name = new Person(name);

          The reason i ask is that I want the program to keep making these objects until the loop is broken, so I don't want to set a limited amount of object names by default.
          >
          You are not creating 'named' objects. You are creating multple instances of the class Person each of which contains a 'name' attribute.

          Your 'Person name' is a variable with the name 'name'. You can only create one variable with a given name in the same scope.
          • 2. Re: Ahh i'm stuck on this
            996722
            Ok I seem to understand that but then what can I do to make an undefined number of instances of Person, until the loop is broken
            • 3. Re: Ahh i'm stuck on this
              gimbal2
              >
              You can only create one variable with a given name in the same scope.
              Ai ai ai, this sentence can be interpreted so badly in the context of this particular thread.
              • 4. Re: Ahh i'm stuck on this
                gimbal2
                993719 wrote:
                Ok I seem to understand that but then what can I do to make an undefined number of instances of Person, until the loop is broken
                Perhaps you're not there yet in your studies. A common way to create a whole list of objects is to use an actual List class, ArrayList usually.
                • 5. Re: Ahh i'm stuck on this
                  996722
                  Maybe i should clarify.

                  I'm not trying to make an instance of Person called "name". I'm trying to call the instance the same value of whatever is stored in the "name" string that was defined earlier in the code. Each time the loop goes around, the "name" string changes and should keep creating a new instance of Person based on the name that was entered each time


                  I guess when it boils down to it

                  If this works:

                  String name = testname;
                  System.out.println(name);
                  /*this prints name*/

                  Why wouldnt this work

                  String name = testname;
                  Person name = new Person();
                  /*this thinks i want to make a variable called name*/
                  • 6. Re: Ahh i'm stuck on this
                    gimbal2
                    You are defining a variable name of the type Person. Its right there in your code. You dont want that - it makes more sense that you want to create a person and give that person a name.
                    Person person = new Person(name);
                    Wouldn't you agree?
                    • 7. Re: Ahh i'm stuck on this
                      rp0428
                      >
                      Ok I seem to understand that but then what can I do to make an undefined number of instances of Person, until the loop is broken
                      >
                      You have to have somewhere to put them after you make them.

                      Like gimbal2 suggested you can create a List instance and add each new one you create to the list.
                      List myPersonList = new ArrayList();
                      . . .
                      myPersonList.add(new Person('name1'));
                      myPersonList.add(new Person('name2'));
                      myPersonList.add(new Person('name3'));
                      . . .
                      • 8. Re: Ahh i'm stuck on this
                        doremifasollatido
                        If you need to be able to refer to the Person objects by name, you could use a Map such as HashMap:
                        Map<String, Person> map = new HashMap<String, Person>();
                        System.out.println("Enter persons name");
                        String name = scan.nextLine();
                        /* now i want to make an object for that person using the name that was just entered */
                        map.put(name, new Person(name));
                        System.out.println(map.get(name)); // Will only print nicely if Person has a customized toString() method.
                        • 9. Re: Ahh i'm stuck on this
                          gimbal2
                          doremifasollatido wrote:
                          If you need to be able to refer to the Person objects by name, you could use a Map such as HashMap:
                          Map<String, Person> map = new HashMap<String, Person>();
                          System.out.println("Enter persons name");
                          String name = scan.nextLine();
                          /* now i want to make an object for that person using the name that was just entered */
                          map.put(name, new Person(name));
                          System.out.println(map.get(name)); // Will only print nicely if Person has a customized toString() method.
                          But what if two people have the same name, hm? Don't think it doesn't happen - it happens quite often. There are at least three people on LinkedIn with the exact same name I have.
                          • 10. Re: Ahh i'm stuck on this
                            doremifasollatido
                            gimbal2 wrote:
                            doremifasollatido wrote:
                            If you need to be able to refer to the Person objects by name, you could use a Map such as HashMap:
                            ...
                            But what if two people have the same name, hm? Don't think it doesn't happen - it happens quite often. There are at least three people on LinkedIn with the exact same name I have.
                            That's a fair point, and I agree it will happen often. For "named" variables that the OP wanted, a Map can sometimes be used effectively. For person's names, as you say, that may not be unique enough. In that case, my suggestion may still give him some ideas. For example, you could use my code if you were creating the Person objects with a unique ID [social security number, or whatever, that is specific to a person and guaranteed unique] instead of a name. As long as the OP learns the potential problems (such as non-unique names, as you pointed out), my suggestion may work for his application, which is probably just to learn Java and not to make a full-fledged business system.