0

Let's say:

I have a Class Animal and I want to create an instance of that class by the name "bird". "bird" is stored in a String.

How to do that?

String variable_name = "bird";

I want to use the string in variable_name as an Animal's instance.

Animal bird = new Animal();

Thanks...

5
  • It's not exactly clear what you're asking. Do you want to have a field inside Animal that when accessed would return "bird", or do you want to have the actual instance name of the Animal be based on some value in a String, in this case bird? Commented Apr 25, 2014 at 1:27
  • I want to use the string stored in variable_name as a new variable of type Animal Commented Apr 25, 2014 at 1:36
  • do you want to instantiate a new Animal and assign it to the field with name 'variable_name' ? Commented Apr 25, 2014 at 1:37
  • assign it to the field with name 'variable_name' Commented Apr 25, 2014 at 1:40
  • why do you want to use the string to determine the variable name? the name of a local variable doesn't affect code outside of the block where it is declared. Commented Apr 25, 2014 at 1:57

2 Answers 2

2

You can do this with the reflection

public class DynamicSetTest {

    private Animal animal1 = null;
    private Animal animal2 = null;
    private Animal animal3 = null;

    public Animal getAnimal1() {
        return animal1;
    }
    public Animal getAnimal2() {
        return animal2;
    }
    public Animal getAnimal3() {
        return animal3;
    }

    public void setField(String name, Animal value) throws Exception {
            Field field = this.getClass().getDeclaredField(name);
            field.set(this, value);
    }

    public static void main(String[] args) throws Exception {
        DynamicSetTest t = new DynamicSetTest();
        Animal anAnimal = new Animal();
        t.setField("animal3", anAnimal);
        assert t.getAnimal3() == anAnimal;
    }
}

Note that you have a field for each possible name. You also have to handle the case if the variable doesn't exist.

I'm wondering if you instead want to use a Map and add objects using that name like

Map<String, Animal> animals = new HashMap<String, Animal>();
animals.put("animal3", anAnimal);
assert animals.get("animal3") == anAnimal;
Sign up to request clarification or add additional context in comments.

3 Comments

In general, reflection isn't worth the trouble.
I agree, that's why I suggested a map to store objects by name.
Actually that's a great idea.
0

Add name as a field in Animal and ask for it in the constructor.

class Animal {

    String name;

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

Then when you construct an animal use:

Animal bird = new Animal("bird");
Animal deer = new Animal("deer");
Animal wumpus = new Animal("wumpus");

1 Comment

e.g: Animal deer = new Animal("deer"); In your example, you've written Animal deer. but I want to use the string stored in variable_name as a new variable of type Animal Something like: String variable_name = "deer"; Animal variable_name= new Animal();

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.