Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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...

share|improve this question
    
bird and animal?? –  VD' 1 hour ago
    
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? –  aruisdante 1 hour ago
    
I want to use the string stored in variable_name as a new variable of type Animal –  user2919740 1 hour ago
    
do you want to instantiate a new Animal and assign it to the field with name 'variable_name' ? –  Totò 1 hour ago
    
assign it to the field with name 'variable_name' –  user2919740 1 hour ago
show 1 more comment

2 Answers

up vote 2 down vote accepted

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;
share|improve this answer
1  
In general, reflection isn't worth the trouble. –  Anubian Noob 1 hour ago
1  
I agree, that's why I suggested a map to store objects by name. –  Totò 1 hour ago
    
Actually that's a great idea. –  Anubian Noob 1 hour ago
add comment

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");
share|improve this answer
    
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(); –  user2919740 1 hour ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.