1

My question is similar to:

Creating a variable name using a String value.

But I was hoping to take a different angle on it.

The reason for wanting to name each object is so I can call on the specifically later, and create multiple objects using the same statement. Ideally, this is what I want to work.

String dogName = reader.nextLine();

int legs  = reader.nextInt();

char beenFed = reader.nextChar();

new dogName = dog(dogName, legs, fed) `

I know java can't accept the variable as the object name. But suppose I want to create three dogs, "Red", "Blue" and "Jeff", how could I configure it, so that later the user can enter

Red.getLegCount();

Blue.hasBeenFed();

etc.

How else can I create this scenario? If I'm wanting to make many differently named Dog objects, how do I do it? And if it isn't a good idea, what could I do instead?

PS - First post on Stack Overflow. Please guide with etiquette/formatting.

4
  • I see you edited this for me Stuart. Please tell me what I needed to do, so I can post correctly in the future. Commented Apr 21, 2013 at 5:09
  • Stuart just edit your post so your post became more readable especially in code part... Commented Apr 21, 2013 at 5:49
  • please close the question if your doubt has been answered :) Commented Apr 21, 2013 at 18:30
  • I understand my post was edited to become more readable. I would like to know how I could do it in the future. Commented Apr 22, 2013 at 10:52

1 Answer 1

4

Why don't you use a HashMap? What you basically need is a set of key-value pairs. This is exactly what HashMap does. A good link to start off: http://tutorialswithexamples.com/java-map-and-hashmap-tutorial-with-examples/

Make your Dog class with dogName, legs, beenFed, other attributes.. Next, define a HashMap<String,Dog> with key being as dogName and Dog being the entire Dog object with all attributes. e.g

Dog red = new Dog("red",5,true);
hashmap.put(red.dogName, red);

Later if you want to access the Dog object, do olddog = hashmap.get("red"). Now, you can do anything - olddog.getLegs() ...

Sign up to request clarification or add additional context in comments.

4 Comments

+1. A Hashmap or simply a collection of dog objects would suffice. If you have some natural key for your object (like dog name, or combination of properties of a dog), then you can simply grab it from the collection.
IT doesn't seem like objects are being created using HashMap - although maybe I am wrong.
I still need to be able to query a dog object using getLegCount() from the dog class.
I've had a look at HashMap, and that's a good solution, however using a key works too, then using a getKey() function in conjunction with a compare to bring up a specific object. Thanks for your help!

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.