Michał Tabor
Michał Tabor

Reputation: 2467

Creating classes dynamically

I'd like to create new classes dynamically, using Java. I have 3 buttons with labels: 1, 2 and 3. Code is like:

switch (button.getActionCommand()) {
  case 1:
    return new Listener1();
    break;
  case 2:
    return new Listener2();
    break;
  case 3;
    return new Listener3();
    break;
}

And it works but I'd like to make it shorter. Every new class will be different from the previous with last number, only. So is it possible to create classes dynamically like:

return new Listener()+button.getActionListener();

I'm sure its possible, but how? Should I use one of Proxy classes or is there an easier way to achieve this?

Upvotes: 0

Views: 114

Answers (4)

Conffusion
Conffusion

Reputation: 4465

Even when you use Class.forName(...) somewhere in your classpath this Listener1, Listener2, Listener3 classes must exist so this will not work for a random number but only for the numbers wherefor such a corresponding Class is defined. I assume you expect all these different classes respect some "contract" (=interface) so you can define an interface which must be implemented by these concrete classes:

public interface Listener() {
     public void someExpectedMethod();
}
public class Listener1 implements Listener {
    public void someExpectedMethod() {
        // some implementation logic
    }
}

So what you can do is create one a static map of these classes:

static Map<Integer,Class<? extends Listener>> listenerMap=new HashMap<>();
listenerMap.put(1,Listener1.class);
listenerMap.put(2,Listener2.class);

In your button you can:

Listener inst=listenerMap.get(button.getActionCommand()).newInstance();
inst.someExpectedMethod();

It's a little more code but you have:

  • type checking
  • your IDE will find occurrences where Listener1 is used.

Upvotes: 0

ppeterka
ppeterka

Reputation: 20726

Provided this is Java, you could use reflection if you really wanted that...

return Class.forName("packageName.Listener"+theNumber).newInstance();

Of course, doing so might have adverse effects too - performance hit, ugly code, debugging and readability issues, having to deal with handling multiple kinds of exceptions...

Recommended reading:

Upvotes: 2

Alex Oliveira
Alex Oliveira

Reputation: 922

If it's Java, you can do something like this:

String className = "package.Listener" + button.getActionCommand();
Class theClass = Class.forName(className);
Object theListener = the.newInstance();
return (Listener) theListener;

Upvotes: 2

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369430

You didn't specify any language, so here is one way:

const_get("Listener#{button.action_command}").new

However, I don't see why you would need to create classes dynamically for this. From what I understand, the ListenerN classes already exist and you just want to dynamically instantiate them?

If you must create classes dynamically, that's not a problem, either:

const_set("Listener#{button.action_command}", Class.new)

Upvotes: 2

Related Questions