-1

How do I get a list(names) of all the Methods() I have inside my Class in java?

Ex.

public class helloworld {
     public static void main() {
         ...
     }

     public static void method() {
         ...
     }

     public static void anothermethod() {
         ...
     }
}

The result would be:

method
anothermethod

Is it possible? Thanks in advance.

alexandreferris
  • 662
  • 1
  • 11
  • 29
  • 2
    Reflection will do that for you. – Sotirios Delimanolis Oct 09 '13 at 17:42
  • Yes it is possible, search for reflection. – Marcelo Oct 09 '13 at 17:42
  • Please, not `helloworld`, but `HelloWorld`. Not `anothermethod`, but `anotherMethod()`. Follow Java Naming conventions. – Rohit Jain Oct 09 '13 at 17:43
  • Extending on what Zavior said earlier: [this](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getDeclaredMethods()) link to the docs. – Dennis Meng Oct 09 '13 at 17:44
  • Did you look at the spec for java.lang.Class?? – Hot Licks Oct 09 '13 at 17:50
  • I'd like to add, that before you do something like this, be sure that it is actually necessary and there's no other way. From the tutorial on reflection: "Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection." – Cruncher Oct 09 '13 at 17:51

1 Answers1

3

You need to use Reflection. Class#getDeclaredMethods() is what you're looking for.

public void showMethods(Object obj) {
    if (obj != null) {
        Method[] methods = obj.getClass().getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
    }
}

If a list of method names is all you need to print; better add it to a Set first. This would print overloaded methods only once. If you'd like to print method names along with parameter information take a look at Method#getParameterTypes().

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • 2
    While this is the answer, you're just helping OP to become a zombie coder that will ask for everything instead of do an easy search on the net for this. – Luiggi Mendoza Oct 09 '13 at 17:45
  • 1
    @LuiggiMendoza This is the subject of massive debate, but the idea is that we want SO to be the destination when people do a net search, and so it's actually reasonable for people to ask a question on SO even if it's easy to find elsewhere. By all means, if this already duplicates an existing question on SO, then do flag it as a dupe. – C. K. Young Oct 09 '13 at 17:47
  • @LuiggiMendoza I prefer to call them [vampires](http://www.slash7.com/pages/vampires) :-) – om-nom-nom Oct 09 '13 at 17:47
  • Just to know, what parameter I have to receive in showMethods() ? As seen Im new to Java – alexandreferris Oct 09 '13 at 17:49
  • @Alexandre any `Object` maybe? At least test it. – Luiggi Mendoza Oct 09 '13 at 17:51
  • @Alexandre You really shouldn't be using reflection when you're new to java. It should be avoided unless extremely necessary. It's used by frameworks a lot to be able to provide some level of abstraction, in most cases it looks tempting, but you don't actually need it. – Cruncher Oct 09 '13 at 17:54
  • @LuiggiMendoza as said before, IM NEW TO JAVA. Therefore i don't know what to do in many cases, so would be a better answer if you just explained it. – alexandreferris Oct 09 '13 at 17:57
  • @Cruncher Yes, im new to Java but that doesn't mean I can't test everything in it. – alexandreferris Oct 09 '13 at 17:58
  • 2
    @Alexandre I may have been a little unclear. Reflection should always be avoided, regardless of how much you know java. The JVM loses it's ability to do optimizations, and a lot of guarantees are thrown out the window. It should only be used when you have a good reason. If you're just testing out the language is a different story. It's good to know what reflection is, how to avoid it, and when you actually should use it. – Cruncher Oct 09 '13 at 18:01
  • @Alexandre The method needs an Object that belongs to the class whose methods you're interested in. So, to print methods of your own class pass it your class's object `showMethods(new HelloWorld());` – Ravi K Thapliyal Oct 09 '13 at 18:01
  • @Cruncher oh yes, for sure all of this is just so I know a bit of the language. Sorry for missunderstanding. – alexandreferris Oct 09 '13 at 18:03
  • @Alexandre I just wrote "java get class methods" and got [this Q/A](http://stackoverflow.com/questions/5266532/can-i-get-all-methods-of-a-class) as first result. So, being new to a technology is like a lame excuse to not try to do some basic research but instead posting a new Q/A here. – Luiggi Mendoza Oct 09 '13 at 19:03