Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im not sure if its possible, but I have been trying to pass a class as a parameter. After running the code i get a native methods error , what im trying to do is pass a collection of classes and execute a specific method called execute using a stack.

Java
import java.util.EmptyStackException;
import java.util.Stack;


public class Tree {
    Stack<Node> nodeStack = new Stack<Node>();

    public Tree(){
         nodeStack = null;
    }
    void setTasks( Node  Nodes){

        try{



            nodeStack.push(Nodes);
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }
     void runTasks(){

       Node node = (Node) nodeStack.peek();
        if(nodeStack!=null){
              node.execute();


        }

        else{
            throw new EmptyStackException();
        }
    }


}


public abstract class  Node {

    public boolean condition(){
        return false;
    }


    public void execute(){

    }




}
Posted
Updated 2-May-13 19:55pm
v2

YES, it is possible.
Java Reflection API - that will guide you through.
 
Share this answer
 
Comments
diego14567 3-May-13 11:35am    
Thanks ill look into reflection
I think you'll run into some trouble because you're trying to push an object of a abstract class onto the stack... An abstract class can't be instansiated. Also, you're title reads 'class', it should be 'object'. I do agree to look at the reflection API.
 
Share this answer
 
Comments
TorstenH. 3-May-13 9:19am    
you didn't figure it: He's talking about passing a class (if you will a class object) as an argument rather than the well known pojos.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900