Using class.forname in a non static way

Discussion in 'Plugin Development' started by JeroenV, Feb 15, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    JeroenV

    Hello,

    I'm having a small problem with using class.forname to get a class and then try to invoke a non-static method with it. Here's the code I'm using:

    Code:text
    1.  
    2. Class<?> ABclass = Class.forName("Executor."+"ActiveBuildings."+e.getBuildingName()+"."+e.getBuildingName());
    3.  
    4.  
    5. Method meth = ABclass.getMethod("newActiveBuilding");
    6. Ameth.invoke(ABclass.newInstance(),e.getLocation());
    7.  


    Now the method 'newActiveBuilding' is non-static, I've been googling this for a while but I'm unable to find anything on the problem.

    I'm used to make a new instance of it like this:
    Code:text
    1. ABclass instance = new ABclass();

    However, this is not possible because it is a class<?> object.


    EDIT:

    I should note that the method is not in the actual subclass I'm referring to but in its superClass.
    So for example if the class was named Fire:
    Code:text
    1.  
    2. Fire fire = new Fire();
    3. fire.newActiveBuilding(e.getLocation());
    4.  


    If I put this in eclipse, it'll work. I just need to variabilize it. So I need something like this with the code stated above.
     
  2. Offline

    raGan.

    you can invoke constructor of that class to get its object
     
Thread Status:
Not open for further replies.

Share This Page