1

Let's say I have two different classes that declare objects: class Cat and class Dog. They are both objects.

Now in my main class, I want to take in a String value (either "Dog" or "Cat"), and create an object of that type. And I'm not looking to have Cases, I'm somehow looking for a dynamic way to create the object.

For example:

public static void main(String [] args){ 
       String object_name = "Dog"; 

 //this is where I want to dynamically create the object
       object_name bob = new object_name();
}

And in the case, it would create a Dog object of variable name bob.

Thanks for the help.

Edit##

I used the method:

public static Object generate(String type) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {


    Class<?> c = Class.forName(type);
    Constructor<?> ctor = c.getConstructor(String.class);
    Object object = ctor.newInstance("9");

    return object;
}

Now if I create in main:

Object obj = generate("Dog");

How do I invoke methods in the class Dog? Do I have to cast it as Dog? If I have to cast it, doesn't it lose purpose of generating it through this method? Thanks again.

Stevantti
  • 494
  • 2
  • 6
  • 13
  • http://docs.oracle.com/javase/tutorial/reflect/ – Cruncher Nov 15 '13 at 16:53
  • 2
    http://stackoverflow.com/questions/6094575/creating-an-instance-using-the-class-name-and-calling-constructor – Matthew Mcveigh Nov 15 '13 at 16:53
  • What are you trying to achieve, at a higher level? Why would an end-user, not knowing anything about Java and the internals of your program, enter Java class names? – JB Nizet Nov 15 '13 at 16:57
  • Its for an assignment. – Stevantti Nov 15 '13 at 17:02
  • @user2991108 you need to give us a description of the assignment, or we can't help you much anymore. And if you can show that this question is different than the one flagged as a duplicate, then I will case a reopen vote. – Cruncher Nov 15 '13 at 17:08
  • Take in an input of String and create an object of that name. There are 6 different Classes that can be instantiated. The parameters for each of the classes are an array of long values (also inputted). – Stevantti Nov 15 '13 at 17:10

3 Answers3

1

One alternative would be to have Cat and Dog both implement/extend Animal and then do something along the lines of:

public Animal fromString(String name) {
    switch (name.toLowerCase()) {
    case "dog":
        return new Dog();
    case "cat":
        return new Cat();
    default:
        return null;
    }
}

I would try to stay away from reflection if at all possible.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

You can do just that, if I understand you correctly:

String className = "com.mypackage.Dog";
Class<?> myClass = Class.forName(className);
Dog dog = (Dog) myClass.newInstance();
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
NickJ
  • 9,380
  • 9
  • 51
  • 74
  • And that will only work if there is a public constructor with not argument in Dog. – JB Nizet Nov 15 '13 at 16:56
  • How do you know that the object should be a Dog? That would defeat the whole point of using the reflection. – arshajii Nov 15 '13 at 16:56
  • If you remove the casting, and use instanceof, you can check. – NickJ Nov 15 '13 at 16:57
  • The best use of this approach is where you don't know at compile time what class it will be, but you DO know that the class implements a certain interface. You can check with instanceof MyInterface and then cast. – NickJ Nov 15 '13 at 16:59
  • @NickJ instanceof would require an if statement, which we've since learned is not allowed – Cruncher Nov 15 '13 at 17:00
  • Ah yes, just seen the comment. That's not a reasonable constraint at all! – NickJ Nov 15 '13 at 17:05
  • @NickJ But the idea still remains, if you're going to use instance of to determine if the object is a dog, why not just use an if to know which one to instantiate in the first place? – Cruncher Nov 15 '13 at 17:07
0

You can do it with Class.forName() and .newInstance() but unless you have some idea at compile time what the class name will be, you won't be able to cast it to the desired type. So you'll still be left with an Object.

Robert
  • 139
  • 4