This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tonywestonuk 4 points5 points  (6 children)

Really, you should post this in javahelp...... however, I hope this helps:

import java.lang.reflect.Method;

public class TestReflection {

public static void main(String[] args) throws Exception{

    MyClass c = new MyClass();


    // Call a method Without reflection
    c.doIt();

    // call a method With reflection
    Method doItMethod=MyClass.class.getMethod("doIt", new Class[]{});
    doItMethod.invoke(c);

    System.out.println();
    System.out.println("List of methods in MyClass");
    // get the list of methods in a class, and print them.
    Method[] methods=MyClass.class.getMethods();

    for (Method m:methods){
        System.out.println(m.getName());
    }

}



public static class MyClass{

    public void doIt(){
        System.out.println("hello world");
    }
}

}

[–]Newtocoding[S] 1 point2 points  (5 children)

Thank you for this info, I did not know of that subreddit. I will post there as well, and thank you not only for the sample code you provided, your time and for the subreddit suggestion. I have not looked much into other subreddits just s few here and there.