0

I have some classes and for some reason I need a procedure that create some variables of that class dinamically...

Let's assume I have this code:

for (int i = 0 ; i < 5 ; i++)
{
  ....
  POP tmp = new POP();
  tmp = (POP) ast.convert(aso, POP.class);     
}

I want that the POP class is set dinamically... I almost achieved what I want except for the casting from object to class, I do not know how to write it...

String className = "POP";
Class cl = Class.forName(className);
Class cl2 = POP.class;
cl = (??????) ast.convert(aso, cl2);

How can I solve it?

2
  • 2
    Could you provide some more code? To be honest: I'm still kind of guessing what you're trying to achieve here :) Whats the .convert() method doing? What is aso? Commented Jan 24, 2012 at 10:10
  • For this code, ?????? should be Class, but i don't think that's what you wanted :-) You can probably achive what you want by declaring cl to be an interface type which is implemented by your classes. Commented Jan 24, 2012 at 10:20

1 Answer 1

1

In your second code snippet, cl will actually be Class<POP> but cl2 is Class<String>, which I guess is not what you expect. Assuming you are coding in Java 5 or newer, you should not use the raw types (Class instead of Class<?>), this will make your code safer and easier to read.

Also note that you have to use the fully qualified class name, and not just the simple name of your classes. For example

Class.forName("String"); // Won't work
Class.forName("java.lang.String"); // Is what you need.

When you have a Class instance you can use reflection to create new instances dynamically:

    Class<?> cl2 = ... ;        

    // If the class has a no-arg visible constructor
    Object foo = cl2.newInstance();

    // Or using an explicit constructor (here with an integer and a String as arguments)
    Constructor<Class<?>> cons = cl2.getConstructor(Integer.class, String.class);
    Object bar = cons.newInstance(1, "baz");

But maybe what you are trying to achieve could be done using the Abstract Factory pattern ? You provide an object that is able to create instances of the type that you want, and you can choose the factory to use based on the class name.

http://c2.com/cgi/wiki/wiki?AbstractFactoryPattern

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

3 Comments

thank you, you helped me a lot... one last question.. if the class has a method (ie. getdetails() ) how can I call it from the Object foo?!?
You can use getDeclaredMethod() on your class (not the instance !) which will return a Method instance. You can then call invoke on this method instance, using foo as first argument. docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html
But using reflection like this is not necessary in most cases, and is unpractical, unsafe and inefficient. I'm pretty sure what you are trying to achieve could be solved by better means.

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.