-1

I'm new to java, and am having trouble getting my head around the concept of dynamically creating objects. I understand that objects are created at runtime, and have seen methods of dynamic creation using arrays, but this seems a bit convoluted, and I can only assume I am not doing things the 'java way'.

For example, if I have a class Dog:

public class Dog {
    int age;

    public void setAge(int age){
        this.age = age;
    }
}

and i want to create a new dog and set its age:

dog newDog = new dog();

newDog.setAge(3);

Then there is no problem. However, if I want to take user input where I take the dogs name and use that name as the name of the object itself, then I hit a bit of a brick wall. I envisaged something along the lines of:

Scanner sc = new Scanner(System.in);
dog sc.Next() = new dog();

furthermore, not knowing the name of the newly created object ahead of time seemingly makes it impossible to reference it (e.g. for a setter method).

As I said, I am convinced that my brain is just not thinking in java. I'm more used to programming in a linear fashion with vba or python, and I think OOP has my head spinning in circles. Any resources that anyone could point me to that might help me see the wood for the trees would be much appreciated. I've scoured the web in an effort to find something with no success thus far.

  • Scan the input into a separate variable, pass this to the setAge method of an instance of Dog – MadProgrammer Dec 25 '17 at 01:33
  • 1
    When you declare a variable like `Dog x;`, then `x` is the name of the variable that can refer to a Dog. However, in Java (and most other programming languages) it is *not* possible to use a *string* as a variable name. So you can not let the user enter a string like `"pluto"` and then magically have a variable that you can use like `Dog "pluto" = new Dog()`. Did I understand this question correctly? (If so: Something similar can be achieved with a `Map`, but that's beyond the scope of this comment) – Marco13 Dec 25 '17 at 01:43
  • (BTW: I think that Python in fact **is** one of the few languages where you can basically "let the user enter a variable name" - that's why I suspect that this is what you may be looking for...) – Marco13 Dec 25 '17 at 01:44
  • The fundamental misunderstanding here is that an **object**, like the instance resulting from `new Dog()`, is district from a **variable** which refers to that object. Variables have names, objects don't. – Daniel Pryden Dec 25 '17 at 01:51

1 Answers1

0

However, if I want to take user input where I take the dogs name and use that name as the name of the object itself

In Java, you cannot read user input and use that as an identifier (variable name) for an object. Rather you'll need something like this:

Scanner sc = new Scanner(System.in);
String name = sc.nextLine(); // read the user input

Dog dog = new Dog();  // create a Dog instance where dog is the identifer
dog.setName(name);  // set the dog name provided by the user 
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 1
    The part with *"I take the dogs name and use that name as the name of the object itself"* sounds more like what people can do in other programming languages - namely to basically use an arbitrary `String` **as** a variable name. – Marco13 Dec 25 '17 at 01:39
  • @Marco13 thanks for that. edited to provide more details as to what you pointed out. – Ousmane D. Dec 25 '17 at 01:44
  • Until now, this was just a guess - I added some comments to the question, in order to figure out whether this really was the intention of the asker.... – Marco13 Dec 25 '17 at 01:45
  • @Marco13 Alright... but seems to make sense as to what you've mentioned though considering OP is trying to do `dog sc.Next() = new dog();` – Ousmane D. Dec 25 '17 at 01:47