Making an object for a class based on a variable (is it even possible?)

Hey, sorry if this doesnt make much sense, but I cannot explain it very well.

so is it possible, and if so how, to make an object based on a variable. so for example. I have a string and the string is currently “Foo” is there a way to then make “new Foo foo;” based on that?

but of course I cant just do “new Foo foo;” The class has to be whatever the string is.

Sorry if this makes absolutely no sense, or if I am going about this the wrong way.

Object fooInstance = Class.forName(fooString).newInstance();

I kinda get it, but obviously the question would be “why ?”
apart from that: if the fields of the new object have names, according to some string values… you couldn’t even use the new object / its fields, because at compile time there would be no fields / you wouldnt know its names yet - so you couldnt use it

sounds like dynamically named variables… I don’t know

but obviously what you really could do is to have a class with a hashmap, and then use that

Reflection will do it. Keep in mind that invoking methods with Reflection is slow. But if you create all your class in a place where speed doesn’t matter then call the methods from a variable or stored collection the speed is normal.


public interface Shape {
  public void draw();
}
public class Triangle implements Shape {
  public void draw() {
    ...
  }
}
public class Square implements Shape {
  public void draw() {
    ...
  }
}
public class Program {
  ArrayList<Shape> shapes = new ArrayList<Shape>();
  public static void main(String args[]) {
    ArrayList<String> names = //load all your strings here
    for(String name:names) {
      //exceptions left out for brevity
      //invoke constructor method is slow
      shapes.add(Class.forName(name).newInstance());
    }
    for(Shape shape:shapes) {
      //since this part invokes the draw method without reflection the speed is normal
      shape.draw();
    }
  }
}

I think that rivens method is exactly what I am looking for, thanks!

as for the why, it is because I need to make a new object to be added to a list, and which class it is is based on that string.

It’s very useful for loading implementations of interfaces

Scripting interfacing and/or hot deployment are a couple of examples.

Hey, I tried rivens way, and I got an error saying that the class did not exist, when I am certain it does, so ist here any special things I have to have in the string like .class or soemthing?

google FQCN

You need the full class name (ie. with the package as well). So something like “java.util.ArrayList”, not just “ArrayList”.

I tried that too, and it didnt work right. maybe i messed something up, I’ll give it another look.

EDIT:

Turns out I had the class name right, but I was just getting a different error, java.lang.InstantiationException.

a quick google says that it is caused when the class is an interface or an abstract, which my class is not :P. any thoughts?

thanks

If your class has a non-default constructor, you need:

Class.forName(fqcn).getConstructor(...).newInstance(...)

oh, so if it extends something like mine does?

I’ll try that, thanks :slight_smile:

well, whether it extends something has nothing to do with it. just read the javadoc, and google a bit, if it’s not clear.